[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4488
  • Last Modified:

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
  • 2
1 Solution
The easiest way is to move to the last row of the resultset and
get it's row number as
count = rs.getRow();
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...  ;-)

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.

 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

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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now