File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » JSP
Bookmark "Is it correct(Hashmap in jsp)" Watch "Is it correct(Hashmap in jsp)" New topic

Is it correct(Hashmap in jsp)

raji navaneethan

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

<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>


try {
int loop;
int loop1;
HashMap hmInner = null;
int intCount = 0;
String strElement = "";
rs = db.execSQL("select * from employ"); %><%
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><%

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

if (!strElement.equals(""))


if (hmInner.size() > 0)



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");
<td width="35%" align="left" bgcolor="#E5ECF9"><b><%salary = (String) hmResult.get("salary");


package SQLBean;
import java.sql.*;

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

public DbBean(){

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

public void close() throws SQLException

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.

wood burning stoves
subject: Is it correct(Hashmap in jsp)