當輸入的用戶存在於數據庫中時,結果集的if
部分會運行,但如果不存在,則else
部分不會。爲什麼?使用數據庫進行登錄驗證
這是我的代碼。
String thisname=request.getParameter("username");
String thispwd=request.getParameter("password");
//Query for selecting user name and password from database.
String query = "select * from login where username=?";
pstmt = (PreparedStatement) con.prepareStatement(query);
pstmt.setString(1, thisname);
res = pstmt.executeQuery();
while(res.next())
{
System.out.println("hhh");
if ((thisname.equals(res.getString("username"))) && (thispwd.equals(res.getString("password"))))
{
System.out.println("hello");
}
else
{
System.out.println("in");
session.invalidate();
response.sendRedirect("index.html");
}
}
因爲'res'具有空結果集,'res.next()'返回false。沒有執行'while循環' –
考慮使用JAAS在Servlet中進行認證和授權。 – Puce