In the following java class i’m having a method authenticate, in which i’m using resultSet.next() method to check that whether the given userName and password exist in the database or not, but it is returning false even when the given userName and password exist in the database.
public boolean authenticate(String userName,String password){
//db connection code
try {
String query = "select user_name from registeredUser where user_name= ? AND password = ?";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, userName);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if(rs.next()) {
System.out.println("True");
return true;
}
else return false;
} catch (Exception e) {
e.printStackTrace();
return False;
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Depending on your underlying DBMS you might have a problem with the case of username and password.
For most DBMS
MySecretPasswordis a different value thanmysecretpassword.So, in case your DBMS is case-sensitive and the user did not enter the username and password exactly the same it’s stored in the database it is very likely that the SELECT returns nothing.