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

Problem getting the number of rows in a ResultSet

 
Chad Schmidt
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I am having difficulties getting the number of rows contained in a ResultSet.
I am using the following code to obtain this data.
NOTE: numRows is an class scope integer variable.
// get the number of rows of the result set
// move cursor to the last row in the result set
rs.last();
// returns the number of the last row
numRows = rs.getRow();

// before processing result set the cursor before the first row
rs.beforeFirst();
When I execute this code I recieve the following error when the rs.last() method fires:
java.sql.SQLException: Cursor state not valid.
NOTE: I am not calling the rs.next() method before this call to rs.last().
Does anyone know why this is happening?
Thanks in advance,
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works fine with Oracle 8.1.7. What database and driver are you using, and show how you create your Statement.
 
kishore harindran
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while creating statement use the following syntax :
conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE , ResultSet.CONCUR_READ_ONLY);
 
Joe Ess
Bartender
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What database and driver are you using,
[ November 17, 2003: Message edited by: Joe Ess ]
 
Chad Schmidt
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,
Sorry for the late reply. I've been out of the office for a few days.
I am using the AS400JDBCDriver as provided by the JTOpen 4.1 release.
Here's how I create my statement and fire off the SQL query:
statement = JDBCconnection.createStatement();
String sQL = "SELECT PRTAX#, PRPNAM FROM CSLIB/PROVIDER WHERE PRSTAT = 'MO'";
rs = statement.executeQuery(sQL);
I have had no problems using the AS400JDBCDriver until now.
I'll try Kishore's suggestion and use the following syntax while creating my statement:
conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE , ResultSet.CONCUR_READ_ONLY);
I'll let you all know who it goes.
Thanks again,
Chad
 
Chad Schmidt
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,
Kishore's suggestion worked when I used the below code. Thank you so much for the suggestion.
statement.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE , ResultSet.CONCUR_READ_ONLY);
I see what I was doing wrong now.
Thanks again,
Chad
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic