• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Invalid cursor state exception problem from resultset

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic