• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Invalid Cursor State!!

 
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);
}
 
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
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic