This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes Invalid cursor state exception problem from resultset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Invalid cursor state exception problem from resultset" Watch "Invalid cursor state exception problem from resultset" New topic
Author

Invalid cursor state exception problem from resultset

abhinav singhal
Greenhorn

Joined: Jun 19, 2003
Posts: 29
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Aug 12, 2004
Posts: 32
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

Joined: Jun 19, 2003
Posts: 29
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invalid cursor state exception problem from resultset
 
Similar Threads
Java Beans and JSP
bean error
JSP validation
What is the problem with this code!!!
problem with jsp page and java bean