Re: JDBC: Fast way to find the number of rows in a ResultSet?
- From: "joeNOSPAM@xxxxxxx" <joe.weinstein@xxxxxxxxx>
- Date: 21 May 2007 11:42:59 -0700
On May 21, 7:46 am, Ronald Fischer <rona...@xxxxxx> wrote:
After a SELECT query via JDBC, is there a fast way to find out the
number of
rows in the result? Of course I could do something like:
Statement s = ...;
ResultSet r = s.executeQuery("select .....");
int rows=0;
r.first();
while(!r.isLast())
{
++rows;
r.next();
}
// The query resulted in 'rows' lines
but this seems to be a bit cumbersome. I also checked the
ResultSet.getMetaData() method, but it seems to provide only the number
of
columns, not the number of rows...
Regards,
Ronald
--
Ronald Fischer <rona...@xxxxxx>
Posted viahttp://www.newsoffice.de/
I wish there was a JDBC FAQ. This would have been the first question.
It's been asked since JDBC and before. Ultimately there is no way to
know. For some DBMSes and for some queries, the DBMS starts sending
the data back to the client before it knows how many rows will
qualify.
You will have to ask the DBMS to send you the number of rows that will
qualify, in a separate query that precedes the real data. Depending on
what applications are doing to the data, you might have to lock all
the
data (not easy or fast in some cases) to ensure that nothing changes
between your 'how-may' query and your send-me-the-data query.
So, the practical solution, if you *must* know, is to buffer the
rows in the client (either in application or an insensitive scrollable
result set or rowset). That will allow the client to know exactly
how many, at the cost of the memory to store it all ahead of time.
That strengthens the best-practices recommendation that you never
read more data to the client than you absolutely need at that moment.
Joe Weinstein at BEA Systems
.
- References:
- JDBC: Fast way to find the number of rows in a ResultSet?
- From: Ronald Fischer
- JDBC: Fast way to find the number of rows in a ResultSet?
- Prev by Date: JDBC: Fast way to find the number of rows in a ResultSet?
- Next by Date: Here you find content in Java !
- Previous by thread: JDBC: Fast way to find the number of rows in a ResultSet?
- Next by thread: Re: JDBC: Fast way to find the number of rows in a ResultSet?
- Index(es):
Relevant Pages
|
|