• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invalid cursor state exception problem from resultset

 
abhinav singhal
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
For the below mentioned code I am getting the invalid cursor state when the query does not return any result from the database.Please let me know how to tackle this situation so that invalid cursor state can be handled



private String queryLogin;
private String dbUsername;
private String dbPassword;

Connection connection=new ForumConnection().getConnection();
ResultSet resultset=null;
/*
** Writting and executing query for getting usernames and respective password from Login table
*/
queryLogin="select * from Login where username='"+username+"';";

try {
Statement statement=connection.createStatement();
resultset=statement.executeQuery(queryLogin);

resultset.next();
if(resultset!=null){
/* Here the Invalid cursor state exception is thrown */
dbUsername=resultset.getString("Username");
dbPassword=resultset.getString("Password");
}

} catch (SQLException e) {
System.out.println(" Query problem for Login--"+e);
}

if(dbUsername.equals(username) && dbPassword.equals(password) ){
return true;
}
else{
return false;
}

Basically the above code takes the username and password as input string and checks itin the database.If it doesn't exists then it return false.

Regards,
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the resultset is empty, rs.next will return false and you cannot read any data from it - there is nothing to read and you get an illegal cursor position because you're not 'looking' at anything.

ie you should have something like this:


The username and pasword won't be set, but that will be because the query failed.

Dave
 
Priyaa V Kumar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code

resultset.next();
if(resultset!=null){
/* Here the Invalid cursor state exception is thrown */
dbUsername=resultset.getString("Username");
dbPassword=resultset.getString("Password");
}

You have done the checking as resultset!=null. Instead modify your code like this

if(resultset.next()){
dbUsername=resultset.getString("Username");
dbPassword=resultset.getString("Password");
}

Look at explanation for ResultSet's next method in API.
 
abhinav singhal
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply and I am sorry I realised later on it by method's signature only that this method returns Boolean and i was handling null and notnull :-).

Anyways, Thanks again for the response.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic