File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Invalid Cursor state- Please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Invalid Cursor state- Please help" Watch "Invalid Cursor state- Please help" New topic

Invalid Cursor state- Please help

Luke Chadwick

Joined: Mar 05, 2004
Posts: 8
A simple problem, which is to hard for me to solve. Im trying to get my program to make sure that duplicate records arent added to a database. I've created a select query (see below) which brings in the values from the selected options, when the add record button is clicked. Where roomNumberValue, courseCodeValue etc are index values of combo boxes.
String queryAddRecord = "SELECT * FROM Lessons WHERE " +
"RoomNumber ='" + roomNumberValue + "' AND " +
"CourseID ='" + courseCodeValue + "' AND " +
"StaffID ='" + staffMemberValue + "' AND " +
"LessonTimeStart ='" + lessonStartTimeValue + "' AND " +
"LessonTimeEnd ='" + lessonEndTimeValue + "' AND " +
"LessonNameID = '" + lessonNamesValue + "' AND " +
"LessonDay = '" + lessonDayValue + "' AND " +
"LessonDateStart ='" + txtLessonStartDate.getText() + "' AND " +
"LessonDateEnd ='" + txtLessonEndDate.getText() + "'";
ResultSet rs = statementAddRecord.executeQuery( queryAddRecord );
display( rs );
When I try to add the record into the DB brings up a SQL cursor error "Invalid cursor state" on the * line
public void display (ResultSet rs)

int recordNumber = rs.getInt(1); *INVALID CURSOR STATE HERE

if(recordNumber !=0)

txtaOutput.append("\nInvalid entry, Record already exists");
else //Add record no duplicates values
//Add record

Please help me.
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Hi Luke,
generally when you want to determine whether a row exists or not in a table, you do something similar to:

What is happening in your code is that a certain selection of your where clause is returning zero results (ie. no record was found in the database, the result-set is empty), so it is invalid to request a column out of the result set, hence the invalid cursor state error.
matthew weeks

Joined: Feb 23, 2004
Posts: 15
Cheers I've got it working.
I agree. Here's the link:
subject: Invalid Cursor state- Please help
It's not a secret anymore!