0

I am developing an application and I want to know the number of results a query extract. the way now i do is giving the result set and execute a loop all over it to find the size but i think this way can not be very effective.(unfortunately rs has not any .size() method) is there any better method that i can use?

CoderInNetwork
  • 2,923
  • 4
  • 22
  • 39
  • 1
    Maybe show an example of a result and what the correct answer for "number of results" for it, so we can figure out what you mean? – wallyk Aug 16 '12 at 17:59
  • See [this](http://stackoverflow.com/a/192104/473637) answer. – Jeshurun Aug 16 '12 at 18:03

3 Answers3

1

One way is to call SELECT COUNT in the query:

SELECT COUNT(*) AS COUNT FROM MYTABLE WHERE...

int count = rs.getInt("COUNT");
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

The most efficient way is to do a select count(*) from table query.

Sarel Botha
  • 12,419
  • 7
  • 54
  • 59
0

A ResulSet is a reference to the first row of a cursor in memory and every record is fetched/loaded into memory one at the time. There is no way to know how many records a Resultset constains, except for looping all over the Resulset or returning a SQL count from the database.

davidmontoyago
  • 1,834
  • 14
  • 18