This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JDBC and the fly likes java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state" Watch "java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state" New topic
Author

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

anna george
Greenhorn

Joined: Nov 18, 2010
Posts: 1
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

Joined: Oct 26, 2010
Posts: 98
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

Joined: Oct 27, 2005
Posts: 19685
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2497
    
    8

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.


OCUP UML fundamental and ITIL foundation
youtube channel
 
Consider Paul's rocket mass heater.
 
subject: java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state