Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invalid Cursor state- Please help

 
Luke Chadwick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers I've got it working.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic