1

Im trying to write a function for authentication using JDBC.My function is

 boolean authenticate(String enteredPassword)throws SQLException{
        String query="select password from Manager where email=?";
        PreparedStatement stmt=con.prepareStatement(query);
        stmt.setString(1,"ivedantlodha@gmail.com");
        ResultSet rs=stmt.executeQuery();
        rs.next();
        String password=rs.getString(1);
        if(enteredPassword.equals(password))
            return true;
        return false;

    }

The function throws SQLException Exhausted ResultSet. The output of rs.next() is false. On executing the following query on sql plus using statement

select password from Manager where email='ivedantlodha@gmail.com'

I am getting a valid output. Here is the output of desc Manager

 Name                      Null?    Type
 ----------------------------------------- -------- 
 NAME                           VARCHAR2(20)
 MANAGER_ID                     NUMBER(38)
 EMAIL                          VARCHAR2(50)
 PASSWORD                       VARCHAR2(30)
  • the question is more about whether the query `select password from Manager where email='ivedantlodha@gmail.com'` is empty ... you should check if the email matches any data – UninformedUser Oct 28 '18 at 12:34
  • Since you can query the same value successfully in SQL\*Plus, did you insert that row and query in the same session, without committing? If so then the other session, from Java, won't be able to see that new row yet. (And what Eran said...) – Alex Poole Oct 28 '18 at 12:45
  • @AlexPoole thanks .I didnt know that it wont work if not committed.Issue resolved. – vedant lodha Oct 28 '18 at 13:15

1 Answers1

2

It looks like you are ignoring the value of rs.next(). You should write something like:

ResultSet rs=stmt.executeQuery();
if (rs.next()) {
    String password=rs.getString(1);
    if(enteredPassword.equals(password)) {
        return true;
    }
}
return false;

Perhaps you don't have a row with email of "ivedantlodha@gmail.com" in your Manager table.

Eran
  • 387,369
  • 54
  • 702
  • 768