0

The login page displays an error message indicating failure to login, even when the correct username and password combination is entered. What is the cause of this problem?

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();
}
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
tebogo
  • 1
  • 2
  • With this code it's impossible that you got an error page with "Sorry! You have entered an invalid username or password. Please try again". It would have thrown a HTTP 500 internal server error. – BalusC Mar 01 '11 at 12:32

1 Answers1

5

it is because

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

stmt is null

Few suggestions for your code:

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438