aspose file tools*
The moose likes JDBC and the fly likes Problem getting the number of rows in a ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem getting the number of rows in a ResultSet" Watch "Problem getting the number of rows in a ResultSet" New topic
Author

Problem getting the number of rows in a ResultSet

Chad Schmidt
Greenhorn

Joined: Nov 04, 2003
Posts: 16
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,


Chad Schmidt<br />ctschmidt
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8971
    
    9

Works fine with Oracle 8.1.7. What database and driver are you using, and show how you create your Statement.


[How To Ask Questions On JavaRanch]
kishore harindran
Greenhorn

Joined: Dec 04, 2002
Posts: 14
while creating statement use the following syntax :
conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE , ResultSet.CONCUR_READ_ONLY);
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8971
    
    9

What database and driver are you using,
[ November 17, 2003: Message edited by: Joe Ess ]
Chad Schmidt
Greenhorn

Joined: Nov 04, 2003
Posts: 16
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

Joined: Nov 04, 2003
Posts: 16
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem getting the number of rows in a ResultSet