help!!!! jsp deletion issues

[nobr]hi

i have made a table using jsp to show the contents of a table from a database in a tabular format. the table rows have two options edit and delete. edit will show all the data in textboxes. this some how works.

delete option is giving one bug and one error

error: ResultSet is closed

bug: the data is being deleted. but instead of the row i want to be deleted,

only the first row is being deleted.

i am using SQLServer 2000 as my database;

i am using Apache Tomcat 5.5.17 as my webserver;

here is my code:

<%@ page errorPage="err.jsp" %>

<%@ page import="java.sql.*;" %>

<%

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection("jdbc:odbc:empdsn");

Statement st= con.createStatement();

Statement st2= con.createStatement();

//Statement st= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

ResultSet rs= st.executeQuery("select * from employeetable");

%>

<html>

<head>

<title>Employee Details</title>

</head>

<style>

.myLnk

{

color:#FF0000;

text-decoration:none;

}

.myLnk:hover

{

color:white;

text-decoration:underline;

}

</style>

<body bgcolor="#B7CEEC">

<table align="center">

<tr bgcolor="#808000">

<th>employee id</th>

<th>employee name</th>

<th>employee address</th>

<th>employee salary</th>

<th colspan="2">options</th>

</tr>

<%

String color="";

String act=request.getParameter("act");

String id=request.getParameter("id");

int i=1;

while(rs.next())

{

String empId=rs.getString(1);

if(i%2==0) color="#0000A0";

else color="#0000FF";

if(act!=null && id!=null)

{

if(act.equals("edit"))

{

%>

<form action="" name="empform">

<table align="center">

<tr>

<td> <input type="text" name="textid" value="<%= empId %>"></td>

<td><input type="text" name="textname" value="<%= rs.getString(2) %>"></td>

<td><input type="text" name="textaddress" value="<%= rs.getString(3) %>"></td>

<td><input type="text" name="textsalary" value="<%= rs.getString(4) %>"></td>

</tr>

</table>

</form>

<%}

elseif(act.equals("delete")){

//rs.absolute(Integer.parseInt(empId));

//rs.deleteRow();

//rs.updateRow();

out.println(empId);

int delres= st.executeUpdate("delete from employeetable where empid='"+empId+"'");

//ResultSet rs1= st.executeQuery("select * from employeetable");

while(rs.next())

{

//String delEmpId=rs1.getString(1);

//int delres= st.executeUpdate("delete from employeetable where empid='"+delEmpId+"'");

%>

<h1><font color="red" size="">DELETED SUCESSFULLY</font></h1><br>

<tr bgcolor="<%= color%>" style="color:white;font-weight:bold;">

<td><%= rs.getString(1) %></td>

<td><%= rs.getString(2) %></td>

<td><%= rs.getString(3) %></td>

<td><%= rs.getString(4) %></td>

</tr>

<%

//st2.executeUpdate(update_table);

}

}

%>

<%}else{ %>

<tr bgcolor="<%= color%>" style="color:white;font-weight:bold;">

<td><%= empId %></td>

<td><%= rs.getString(2) %></td>

<td><%= rs.getString(3) %></td>

<td><%= rs.getString(4) %></td>

<td><a href="showEmployeeTable.jsp?act=edit&id=<%=empId%>" class="myLnk">Edit</a></td>

<td><a href="showEmployeeTable.jsp?act=delete&id=<%=empId%>" class="myLnk">Delete</a></td>

</tr>

<%}

%>

<%

i++;

}

%>

</table>

</body>

</html>

can u please help me fix this? thanx in advance

shankha[/nobr]

[6714 byte] By [shanxstera] at [2007-11-27 7:55:32]
# 1

> bug: the data is being deleted. but instead of the row i want to be

> deleted,

> only the first row is being deleted.

int delres= st.executeUpdate("delete from employeetable where empid='"+empId+"'");

empId value is from rs.next(), rs.getString(1). so it will delete first row.

perhaps you want to do this:

int delres = st.executeUpdate("delete from employeetable where empid ='" + id + "'");

your code is so messy... instead putting list, update, delete in one jsp, seperating them to 3 jsp pages (list/show, update & delete).

j_shadinataa at 2007-7-12 19:36:56 > top of Java-index,Java Essentials,New To Java...
# 2

hi j_shadinata,

thanks for sharing your valuable knowledge with me!

1.

> your code is so messy... instead putting list,

> update, delete in one jsp, seperating them to 3 jsp

> pages (list/show, update & delete).

answer: yes i could have broken it up into 3 jsp pages and it would have been easier. but the requirement was such that all 3 functions of list/show, update & delete had to be done on a single jsp page

2.

> > int delres= st.executeUpdate("delete from

> employeetable where empid='"+empId+"'");

>

>

> empId value is from rs.next(), rs.getString(1). so it

> will delete first row.

>

> perhaps you want to do this:

> > int delres = st.executeUpdate("delete from

> employeetable where empid ='" + id + "'");

>

i dont understand where did id come from. where do i declare this id? could you please explain?

3. how do i fix the error message : "ResultSet is closed "

thanks for all your help!!!

shankha

shanxstera at 2007-7-12 19:36:56 > top of Java-index,Java Essentials,New To Java...