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!! 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!!" 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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Invalid Cursor State!!
 
Similar Threads
invalid cursor state
Servlet, JSP and access violation 0xc 0000005
showing error in jsp that data type value cannot convert to numeric
Help needed with error SQLException: ORA-00928: missing SELECT keyword
Servlet API 2.2