Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invalid Cursor State!!

 
Patrick McDowell
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm getting an Invalid Cursor State SQLexception when I run the update below to an Access Database. The if(!result.next) statement works fine, but the while(result.next()) generates the exception. It successfully inserts the data into the databse, but then fails. Any suggestions?
newsItem lastNewsItem = new newsItem();
String date = request.getParameter("date");
String headLine = request.getParameter("headline");
String abstract1 = request.getParameter("abstract");
String body = request.getParameter("body");
String sqlItemID = "SELECT itemID FROM newsItems WHERE itemID=(SELECT MAX(itemID)FROM newsItems)";
String sqlUpdate;

try
{
stmt = con.createStatement();
rs = stmt.executeQuery(sqlItemID);
while(rs.next())
{
lastItemID = rs.getInt("itemID");
nextItemID = lastItemID + 1;
sqlUpdate = "INSERT INTO newsItems VALUES(" + nextItemID + ", '" + date +
"', '" + headLine + "', '" + abstract1 +
"', '" + body + "', YES)";
stmt.executeUpdate(sqlUpdate);
}
if(!rs.next())
{
nextItemID = 1;
sqlUpdate = "INSERT INTO newsItems VALUES(" + nextItemID + ", '" + date +
"', '" + headLine + "', '" + abstract1 +
"', '" + body + "', YES)";
stmt.executeUpdate(sqlUpdate);
}
con.close();
}
catch(SQLException e3)
{
out.println("Exception3 = " + e3);
}
catch(Exception e4)
{
out.println("Exception4 = " + e4);
}
 
sridevi sangaiah
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
in your while loop you are not moving the cursor in the Resultset to next row.i think you will have to say something like
rs.next()
when i was getting 'invalid cursor state' exception i closed the resultset after retrieving the values from it. i.e.,after while(rs.next()){ }loop
close the rst by saying rs.close();
this should solve the problem.
regards,
sridevi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic