Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

 
anna george
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i'm new to this site .
A first time user .

I hope i'l find answers to my queries please do help me.
I am learning Java for the first time and I am trying to create a login application using Jframe with the editor netbeans.

the following is my code

//code




this is the output i get when i run the program

Connection is created
laconic laconic
Error :java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

here laconic is the value stored in the table. It gets printed in the System .out.println statement but doesnot get stored in a string variable.

Not getting why ? please help

i donot understand why it gives error at the following place in the above program:

userName=rs1.getString(1);
password=rs1.getString(2);

 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is the first calls to getString(1) and getString(2) moved the result set cursor forward and you cannot automatically move it back again just by calling getString(1) and getString(2) again without moving to the next row...but you're already on the last row since you've exited your while(rs.next()) loop. By default a JDBC ResultSet is like an iterator...each call to rs.next() moves the cursor forward one row and you can't move backwards. I would suggest taking a few minutes to read the Javadoc for the ResultSet interface. There is one part in there in particular which says "For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once." You problem is probably related to this in that you are trying to read the same values from the last row of the result set twice. Read the columns once (in your loop) and store them in String reference variables.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Anne!

Could you please UseCodeTags next time? I've added them for you this time. And I'm moving this thread to our JDBC forum where I think you'll have a bit more replies.
 
Jan Cumps
Bartender
Posts: 2596
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are reading (in line 35 and 36) after you closed the while loop in line 34.
At that point, the cursor is beyond the last record.
Rerieving column values is an invalid action in that situation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic