Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / September 2005

Tip: Looking for answers? Try searching our database.

How to count rows using metadata features of JDBC

Thread view: 
surfer dude - 27 Sep 2005 00:25 GMT
I am relatively new to Java/JDBC programming.

I know that you can count columns in a resultset row using the
getColumnsCount() method. However, given a resultset, is there one
simple method that can be called to count the number of rows in the
resultset without looping over each row in the resultset ?

TIA for all your help.
Joe Weinstein - 27 Sep 2005 15:52 GMT
> I am relatively new to Java/JDBC programming.
>
[quoted text clipped - 4 lines]
>
> TIA for all your help.

Hi. There is no way, via metadata, nor any way via any regular query
either. You could do a prior query, eg:

"select count(*) from mytable where ....."
and then do the real query "select what I want from mytable where ..."

But unless you have locked the table, some other process might
alter the contents of the table between the count and the
query. For some DBMSes and query types, the table and results
could change *while* the query was half-processed, so even the
DBMS doesn't know how many rows are going to be returned till
the last row is sent...

The only recourse is to know your data as well as you can,
make as selective a query as you can, and program the client
to handle whatever might come back. There is clever SQL that
will return 50 rows, and then the next 50 rows sucessively
till the data you wanted is done, but the syntax is enough
different for each DBMS that you should ask an expert in your
given DBMS to help write it.

Joe Weinstein at BEA Systems
Bryan E. Boone - 27 Sep 2005 21:48 GMT
>> I am relatively new to Java/JDBC programming.
>>
[quoted text clipped - 27 lines]
>
> Joe Weinstein at BEA Systems

If you have a jdbc driver that has cursor level access you can do:

resultset.last();
int rowCount = resultset.getRow();

be sure to reposition the cursor back to where it was (if it's relevant)
-Bryan
Alfred - 29 Sep 2005 15:24 GMT
> If you have a jdbc driver that has cursor level access you can do:
>
> resultset.last();
> int rowCount = resultset.getRow();

Think that depends on your fetch size. And i'd say it's not a
good idea inside large ResultSet's.

Good luck


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.