Re: number of rows in a resultset

From: Peter Kirk (peter)
Date: 06/09/04


Date: 9 Jun 2004 13:42:12 +0200


"Michael Rauscher" <michlmann@gmx.de> skrev i en meddelelse
news:ca6im8$bn0$01$1@news.t-online.com...
> Liz schrieb:
> > I'm just starting to use JDBC and want
> > to know the number of rows in a result set
> > but have not been able to figure it out.
> >
> JDBC allows the driver to read the results in chunks. That means, that
> the driver needs not to read the complete result set at once. This is
> important due to perfomance and economical memory management. On the
> other hand that means, that there's no way to know the number of all
> records in advance.
>
> If your ResultSet isn't scrollable, then you have to walk through it:
>
> int i = 0;
> while ( rs.next() )
> i++;
>
> But if your ResultSet isn't scrollable, you can't return back, so you
> would have to execute the query twice if you want to know the number of
> records before processing the results.

A possible problem of course being that the data could change between
queries, and the number of rows returned be different.

Also, maybe the first query could be an SQL "count", and the second query
the actual data retrieval. This still has the possible problem of changing
data, and as I am no SQL expert I have no idea if it would provide any
advantage.

Or, you could iterate throug the result set, and create "business objects"
for each row - after this you have your count, and the data to work with.
Depends on your application of course.

Peter



Relevant Pages

  • Re: It is possible to do a JOIN between two ResultSet?
    ... JDBC result sets are simple objects ... that can sometimes be just a wrapper around the socket to the DBMS when ... > the same time have to do another query on a text file. ... > resultset from two different DB (the first a real DB, ...
    (comp.lang.java.databases)
  • Re: It is possible to do a JOIN between two ResultSet?
    ... > I have a program that have to do a query on an Access DB with JDBC, ... > the same time have to do another query on a text file. ... > resultset from two different DB (the first a real DB, ... and use the contents of the Virtual clipboard in another query. ...
    (comp.lang.java.databases)
  • It is possible to do a JOIN between two ResultSet?
    ... I have a program that have to do a query on an Access DB with JDBC, ... resultset from two different DB (the first a real DB, ... SQL query on two tables of the same DB). ...
    (comp.lang.java.databases)
  • Re: why excute query through JDBC much slow than query analyzer?
    ... why excute query through JDBC much slow than query analyzer? ... The SendStringParametersAsUnicode is a connection string attribute that is ... If you capture the Execution Plan in a SQL Profiler trace while executing ...
    (microsoft.public.sqlserver.jdbcdriver)
  • Re: Setting Query Timeout through JDBC (CA Driver problem?)
    ... I suggest to you not using commands like ""set lockmode ..." ... inside jdbc because they are not intended for being used like that nor ... About query timeout we have not used it, but I do not remember any ...
    (comp.databases.ingres)