wood burning stoves*
The moose likes JDBC and the fly likes Invalid Cursor State!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Invalid Cursor State!!" Watch "Invalid Cursor State!!" New topic
Author

Invalid Cursor State!!

Patrick McDowell
Greenhorn

Joined: Aug 15, 2000
Posts: 4

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

Joined: Mar 20, 2001
Posts: 19
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invalid Cursor State!!
 
Similar Threads
Servlet API 2.2
Help needed with error SQLException: ORA-00928: missing SELECT keyword
invalid cursor state
Servlet, JSP and access violation 0xc 0000005
showing error in jsp that data type value cannot convert to numeric