This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Invalid Cursor State!!

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic