I am using ResultSet to fetch the returned records from the database. How can I get the number of rows directly?
I have used the following Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); //for scrollable result set. rs=stmt.executeQuery("SELECT * FROM tab"); int k=rs.getFetchSize();
But it is not working. I am aware of some methods to find the rows fetched like keeping count and travesing rs using rs.next() or quering to database using Count key word in the query to get the records.
But I want to get the size directly. Any help on this issue would be appreciated.
ResultSets can and usually do implement lazy fetching from the database server. That is, they don't have to get all the rows into memory at once. So they can't tell how many rows there are until they get them all. You can try afterLast() and getRow() in some combination, but you'll have to make the rs scrollable in both directions (as you did) to go back and read any data. That will force the rs to hold all data in memory at once, which may be a problem.
Using another query to get the number of matching rows might be good. Of course the count might change between the count query and the real query. My SQL is rusty ... select count(any column) with no where clause? [ June 26, 2007: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
K Kiran Kumar
Joined: Jan 04, 2006
Thanks Stanley for the solution but I am looking for a solution in which we should not even scroll the result set and not fire one more query to fetch the number of records.