Row Count of a ResultSet

Hi !

How can I directly find out how many rows a java.sql.ResultSet in JDBC 1.x returned?
I have to know the row count BEFORE I step through it and I don't want to execute the query twice.

Thanx for your help
Mogwai
mogwaiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shaveriCommented:
The easiest way is to move to the last row of the resultset and
get it's row number as
rs.last();
count = rs.getRow();
0
mogwaiAuthor Commented:
rs.last() is not implemented in JDBC 1.x. It is JDBC 2.x only.

Sorry, but for this idea you don't have to be a genius...  ;-)
0
Ravindra76Commented:


1.The one way is previous shaveri comment.

2. Other way in java 1.x

One way is query the total number of records using count(*).

It is the best solution.

But careful about sessions.

when you query count(*) and after a record is deleted, it will give you nunn pointer exception.

Some synchronization or transactionshould be there.

I am posting some other experts comments.


 Date: Tuesday, January 11 2000 - 02:17AM CST  
 
The database doesn't know the number of rows until the last fetch.
A second query doesn't retrieve the exact number of rows, because there can be inserts/deletes from other sessions.
 

 Comment
 
 From: bkowalski
 Date: Tuesday, January 11 2000 - 02:23AM CST  
 
If you are using a second query to retrieve the exact number of rows, you can set your transaction as ready only to get consistent point in time results and not be affected by inserts/deletes from other sessions.  T

his is if you are not doing any changes to the data.  If you are changing data, you can use the serializable transaction isolation level or lock the entire table.

Best of luck

 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mogwaiAuthor Commented:
Thanx ravindra76.

Inserts or deletes between the two queries are the reason, why I don't want to execute the query twice (We expect about 100 inserts per second). But I see, it is the only way in JDBC 1.x.

By the way, in JDBC 2, the rs.last() method doesn't work with ALL resultsets. So far I know, there are problems with resultsets with a lot of rows (And my query will return really a lot of rows).

Greetings from Zürich
Mogwai
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.