登录页面输入正确的用户名和密码

时间:2011-03-01 07:24:51

标签: java jsp jdbc

即使输入了正确的用户名和密码组合,登录页面也会显示一条错误消息,指示登录失败。这个问题的原因是什么?

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:mydsn","sa","password@123");
String sql = "select customer_id, fname, lname, email_id, phone_number from customer_details where user_name=? and password=?";
PreparedStatement stmt;
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
session.setAttribute("customerId", rs.getString(1));
session.setAttribute("userName", userName);
session.setAttribute("fName", rs.getString(2));
session.setAttribute("lName", rs.getString(3));
session.setAttribute("emailId", rs.getString(4));
session.setAttribute("phoneNumber", rs.getString(5));
conn.close();
%>
<jsp:forward page="welcome.jsp" />
<%} else {%>
Sorry! You have entered an invalid username or password. Please try again.
<%}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

1 个答案:

答案 0 :(得分:5)

这是因为

PreparedStatement stmt;
ResultSet rs = stmt.executeQuery();

stmtnull

您的代码很少有建议: