Meaningless Drivel is fun!*
The moose likes JDBC 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
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Invalid Cursor state- Please help" Watch "Invalid Cursor state- Please help" New topic
Author

Invalid Cursor state- Please help

Luke Chadwick
Greenhorn

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 );
statementAddRecord.close();
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)
{
try
{

rs.next();

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.
James.
matthew weeks
Greenhorn

Joined: Feb 23, 2004
Posts: 15
Cheers I've got it working.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invalid Cursor state- Please help