How do you find the number of records returned using the JDBC API? Is there a direct function call(like in other languages)? Right now we have to loop over the resultset to get the number(I guess the only way)
Created May 4, 2012
Dieter Wimberger Well java.sql.ResultSet does not offer any method to retrieve the amount
of rows that have been selected.
Now I have following ideas that I hope might help:
Now I have following ideas that I hope might help:
- It is recommended to retrieve the results into an abitray datastructure, especially in case of pooled connections. Now ensure using a datastructure that has an accessor method for its size.
- Another possibility would be to use a seperate prepared statement
that returns nothing but a count of the rows that will be selected.
i.e.select count(*) from myTable
Instead of * you can also use any column existing in myTable. - A third way I could think of is to add the count(<column>) to the
request, thus retrieving the count of all rows as the first column of each row.
i.e.select count(<existing column>),* from myTable
I am not sure if it really makes sense, but it's at least a possibility.