aspose file tools*
The moose likes JSP and the fly likes Is it correct(Hashmap in jsp) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Is it correct(Hashmap in jsp)" Watch "Is it correct(Hashmap in jsp)" New topic
Author

Is it correct(Hashmap in jsp)

raji navaneethan
Greenhorn

Joined: Apr 06, 2006
Posts: 15
Hi, am new to this field.I haven't idea about Hashmap.But i wrote this pgm it is perfectly working(Fetching the data from database).But I don't know abt this process is correc or not.
Anyone help me .Urgently I need this one

Thanx in advance

<HTML>
<HEAD><TITLE>DataBase Search</TITLE></HEAD>
<BODY leftmargin=300>

<%@ page language="Java" import="java.sql.*" %>
<%@ page import="SQLBean.*" %>
<%@ page import ="java.util.*"%>
<%ArrayList alReturn = new ArrayList();%>


<jsp:useBean id="db" scope="application" class="SQLBean.DbBean" />
<jsp:setProperty name="db" property="*" />
<%!
ResultSet rs = null ;
ResultSetMetaData rsmd = null ;
int numColumns ;
int i;
String empno = "";
String age = "";
String fname = "";
String salary="";
%>


<h2> Results from Database</h2>

<br><br>

<%
try {
int loop;
int loop1;
HashMap hmInner = null;
int intCount = 0;
String strElement = "";
db.connect();
rs = db.execSQL("select * from employ"); %><%
db.connect();
i = db.updateSQL("UPDATE employ set fname = 'vijaya' where empno='vss09'");
out.println(i+"records are inserted");%><br><br>
<%rsmd = rs.getMetaData();
intCount = rsmd.getColumnCount();
out.println("Number of Columns "+intCount);%><br><br>

<%for (loop1 = 1; loop1 <= intCount; loop1++)
{%>
<b><%out.println(rsmd.getColumnName(loop1));%>      </b><%
}%><br><br>

<%
while(rs.next())
{
hmInner = new HashMap();
for (loop = 1; loop <= intCount; loop++)
{
strElement = rs.getString(loop);
if (strElement == null)
{
strElement = "";
}

if (!strElement.equals(""))
{
hmInner.put(rsmd.getColumnName(loop),strElement);
}

}


if (hmInner.size() > 0)
{
alReturn.add(hmInner);
}

}

}


catch(SQLException e)
{
throw new ServletException("Your query is not working", e);
}
%>
<%
int iSize = 0;

HashMap hmResult = null;

if(alReturn!=null & alReturn.size() > 0){

iSize = alReturn.size();
out.println("Number of Rows "+iSize );

for(int i = 0; i < iSize; i++){

hmResult = (HashMap)alReturn.get(i);
%>
<table cellpadding="2" cellspacing="1" border="0" width="61%"><tr><td width="35%" align="left" bgcolor="#E5ECF9"><b>
<%empno = (String) hmResult.get("empno");out.println(empno);%></b></td><td width="35%" align="left" bgcolor="#E5ECF9"><b>
<%age = (String) hmResult.get("age");out.println(age);%></b></td><td width="35%" align="left" bgcolor="#E5ECF9"><b>
<%fname = (String) hmResult.get("fname");
out.println(fname);%></b></td>
<td width="35%" align="left" bgcolor="#E5ECF9"><b><%salary = (String) hmResult.get("salary");
out.println(salary);%></b></td>
</tr></table><%}}
%>


</body>
</HTML>



package SQLBean;
import java.sql.*;
import java.io.*;

public class DbBean implements java.io.Serializable{
private String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
private Connection dbCon;

public DbBean(){
super();
}

public boolean connect() throws ClassNotFoundException,SQLException{
Class.forName(dbDriver);
dbCon = DriverManager.getConnection("jdbc dbc:mybean","","");
return true;
}




public void close() throws SQLException
{
dbCon.close();
}


public ResultSet execSQL(String sql) throws SQLException{
Statement s = dbCon.createStatement();
ResultSet r = s.executeQuery(sql);
return (r == null) ? null : r;
}

public int updateSQL(String sql) throws SQLException{
Statement s = dbCon.createStatement();
int r= s.executeUpdate(sql);
return (r == 0) ? 0 : r;
}

}
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Well, coding scriptlets (and especially longer ones) is strongly discouraged in favour of using JSTL in combination with servlets.

Creating static variables in JSPs is extremely dangerous. Essentially you're working with globals, which can have very nasty consequences when you have multiple simultaneous requests.

You're creating a new database connection for every database request (of which you have several per HTTP request), and never closing them. MASSIVE nono!
Extremely slow, drains resources like there was no tomorrow (try for example running it against a server on which you only have a license for 5 connections, your third page request will go seriously wrong), and completely unnecessary.


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it correct(Hashmap in jsp)
 
Similar Threads
please figure this out..
exception java.sql.SQLException: Exhausted Resultset
exception java.sql.SQLException: Exhausted Resultset
Want to convert my code in method
JSP Help Regarding the Radio Button