主题:关于jsp中用链接传递参数的问题
我编写的jsp程序目的是在页面中显示数据库中的表格,并且在每行数据最后面有一个X作为链接,点击后即会删除这一行的数据.但是我在运行程序的时候,X这个链接以及第一行以后的数据都不显示,也就是说只能显示X这个连接之前的第一行数据.程序如下,请高手指点.
deleteUser1.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY>
<% Connection con;
Statement sql;
ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
out.print("类找不到!");
}
try
{
con=DriverManager.getConnection("jdbc:odbc:testDB");
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM userTable");
out.print("<Table Border style='font-size: 10pt'>");
out.print("<TR><td colspan=9 align=center>用户数据</td></tr>");
out.print("<TR>");
out.print("<Td width=100 >"+"用户ID号");
out.print("<Td width=50 >"+"用户名");
out.print("<Td width=100>"+"用户真实姓名");
out.print("<Td width=50>"+"年龄");
out.print("<Td width=50>"+"性别");
out.print("<Td width=100>"+"联系地址");
out.print("<Td width=100>"+"联系电话");
out.print("<Td width=100>"+"添加时间");
out.print("<Td width=100>"+"删除");
out.print("</TR>");
while(rs.next())
{
out.print("<TR>");
out.print("<TD >"+rs.getLong(1)+"</TD>");
out.print("<TD >"+rs.getString(2)+"</TD>");
out.print("<TD >"+rs.getString(3)+"</TD>");
out.print("<TD >"+rs.getInt("user_age")+"</TD>");
out.print("<TD >"+rs.getString("user_sex")+"</TD>");
out.print("<TD >"+rs.getString("user_address")+"</TD>");
out.print("<TD >"+rs.getString("user_telephone")+"</TD>");
out.print("<TD >"+rs.getString("add_time")+"</TD>");
out.print("<TD ><a href='deleteUser2.jsp?user_id="+rs.getLong(1)+"'>×</a></TD>");
out.print("</TR>") ;
}
out.print("</Table>");
con.close();
}
catch(SQLException e1)
{
out.print("SQL异常!");
}
%>
</BODY>
</HTML>
deleteUser2.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<%//接收要删除的用户ID号
long user_id;
try
{
user_id=Long.parseLong(request.getParameter("user_id"));
}
catch(Exception e)
{
user_id=0;
}
%>
<%//构造追加记录SQL语句
String sqlString=null;//SQL语句
if(user_id!=0)//接收到的参数正确
{
sqlString="delete from userTable where user_id="+user_id;
//执行SQL语句
try
{ Connection con;
Statement sql;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:testDB");
sql=con.createStatement();
sql.executeUpdate(sqlString);
con.close();
}
catch(SQLException e1)
{
out.print("SQL异常!");
}
}
%>
<head>
<title>用户注册程序</title>
</head>
<body>
<center>
<table border="1" width="700">
<tr>
<td width="100%" colspan="2" align="center">删除用户程序</td>
</tr>
<tr>
<td width="100%" colspan="2">删除用户成功!</td>
</tr>
</table>
</center>
</body>
</html>
deleteUser1.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<HTML>
<BODY>
<% Connection con;
Statement sql;
ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
out.print("类找不到!");
}
try
{
con=DriverManager.getConnection("jdbc:odbc:testDB");
sql=con.createStatement();
rs=sql.executeQuery("SELECT * FROM userTable");
out.print("<Table Border style='font-size: 10pt'>");
out.print("<TR><td colspan=9 align=center>用户数据</td></tr>");
out.print("<TR>");
out.print("<Td width=100 >"+"用户ID号");
out.print("<Td width=50 >"+"用户名");
out.print("<Td width=100>"+"用户真实姓名");
out.print("<Td width=50>"+"年龄");
out.print("<Td width=50>"+"性别");
out.print("<Td width=100>"+"联系地址");
out.print("<Td width=100>"+"联系电话");
out.print("<Td width=100>"+"添加时间");
out.print("<Td width=100>"+"删除");
out.print("</TR>");
while(rs.next())
{
out.print("<TR>");
out.print("<TD >"+rs.getLong(1)+"</TD>");
out.print("<TD >"+rs.getString(2)+"</TD>");
out.print("<TD >"+rs.getString(3)+"</TD>");
out.print("<TD >"+rs.getInt("user_age")+"</TD>");
out.print("<TD >"+rs.getString("user_sex")+"</TD>");
out.print("<TD >"+rs.getString("user_address")+"</TD>");
out.print("<TD >"+rs.getString("user_telephone")+"</TD>");
out.print("<TD >"+rs.getString("add_time")+"</TD>");
out.print("<TD ><a href='deleteUser2.jsp?user_id="+rs.getLong(1)+"'>×</a></TD>");
out.print("</TR>") ;
}
out.print("</Table>");
con.close();
}
catch(SQLException e1)
{
out.print("SQL异常!");
}
%>
</BODY>
</HTML>
deleteUser2.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<%//接收要删除的用户ID号
long user_id;
try
{
user_id=Long.parseLong(request.getParameter("user_id"));
}
catch(Exception e)
{
user_id=0;
}
%>
<%//构造追加记录SQL语句
String sqlString=null;//SQL语句
if(user_id!=0)//接收到的参数正确
{
sqlString="delete from userTable where user_id="+user_id;
//执行SQL语句
try
{ Connection con;
Statement sql;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:testDB");
sql=con.createStatement();
sql.executeUpdate(sqlString);
con.close();
}
catch(SQLException e1)
{
out.print("SQL异常!");
}
}
%>
<head>
<title>用户注册程序</title>
</head>
<body>
<center>
<table border="1" width="700">
<tr>
<td width="100%" colspan="2" align="center">删除用户程序</td>
</tr>
<tr>
<td width="100%" colspan="2">删除用户成功!</td>
</tr>
</table>
</center>
</body>
</html>