这段代码给我一个错误,即结果集关闭后无法执行操作。我不明白出了什么问题。提前谢谢。
<%@page import="java.sql.*"%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
//JDBC CODE
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/**", "****", "******");
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("select * from add_user_table where name='"+username+"' and password='"+password+"'");
if(rs.next())
{
String name=rs.getString("name");
String pass = rs.getString("password");
if(username.equals(name) && password.equals(pass))
{
session.setAttribute("username",username);
response.sendRedirect("any.jsp");
}
else
{
response.sendRedirect"any2.jsp?msg=Invalid");
}
}
else
{
response.sendRedirect("any2.jsp?msg=Invalid");
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
答案 0 :(得分:-2)
我认为这是因为您使用的是if(rs.next),它只会检查一个实例,这可能是您收到ResultSet关闭错误的原因。将其更改为while(rs.next)以扫描每个。
另外,我会在JSP页面中忽略Scriplets,而是选择JSTL(Java服务器页面标记库)和EL(表达式语言)代码来引用任何servlet数据,这将更加整洁和坚持MVC类型架构。