Following code gives me a big headache. Why does my if
ignore rs.next() == true
?
System.out.println(rs.next());
if (rs.next() == true) {
System.out.println("1");
session.setAttribute("userid", userid);
//out.println("welcome " + userid);
//out.println("<a href='logout.jsp'>Log out</a>");
response.sendRedirect("success.jsp");
} else {
System.out.println("2");
out.println("Invalid password <a href='index.jsp'>try again</a>");
}
Console:
#1 SELECT * FROM users where username = 'test' and password = 'test'
#2 true
#3 2
What am I doing here wrong?
What am I doing here wrong?
You're calling next()
twice. The first call returns true
, but presumably the second returns false
- which makes sense, if your query only returns a single result.
If you really want to print the value out for diagnostic purposes, save it in a local variable:
boolean nextResult = rs.next();
System.out.println(nextResult);
if (nextResult) {
...
} else {
...
}
See more on this question at Stackoverflow