how many Rows in a ResultSet ?

HI,How do i know that how many rows are there in a ResultSet?is there any method ?thanks
[116 byte] By [khan_vu2007a] at [2007-11-27 9:03:44]
# 1
No.
cotton.ma at 2007-7-12 21:36:03 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

There is a way to find out though without iterating through the entire result set and incrementing a counter along the way.

Just issue another select beforehand of the form:select count(*) from ( YOUR SELECT STATEMENT HERE )

This will give you a result set with 1 row and 1 column containing a value representing the row-count of the select statement you want to execute.

dwga at 2007-7-12 21:36:03 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
You can get the number of rows in your resultset with the following code : int rowCount = 0;resultSet.last();rowCount = resultSet.getRow();
yyurdusevenna at 2007-7-12 21:36:03 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

True. But *why* do you want to know the rowcount before processing the result? In the data tier the results are generally mapped to a Collection of DTO's which can be used in the presentation tier. Then you can just use Collection#size() to get the rowcount. If you aren't about to do something with the result, then indeed rather gently use a COUNT(*) query and retrieve the rowcount by resultSet.getInt(1).

BalusCa at 2007-7-12 21:36:03 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
Here's a clarifying example how to useselect count(*) from ( YOUR SELECT STATEMENT HERE )select count(*) from person here lastName like 'A%'int count= resultSet.getInt("count");
George123a at 2007-7-12 21:36:03 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...