wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes returning # of rows from sql query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

returning # of rows from sql query

Bill Brasskey
Ranch Hand

Joined: Jun 01, 2004
Posts: 47
does there exist a java function I can call that will return the number of rows returned from a sql select query, so i don't have to "select count(*)"?
...i guess i'm looking for something like the " mysql_num_rows" function in PHP.
Jeffrey Bennett
Greenhorn

Joined: May 17, 2004
Posts: 12
You have two options.

1.) Since the ResultSet interface doesn't provide you direct access to the number of rows, you could fairly easily iterate through it to get the count yourself using the first() and next() methods to traverse ResultSet. A static helper method could easily be created to do this, if need be.

2.) Since the implementation of ResultSet depends on your database vendor, you could make assumptions based on your architecture. e.g. My database is Oracle, therefore ResultSet is really an OracleResultSet. You can then look for the convenience method in the implementation class. From a design standpoint, this creates an awkward coupling that you may not wish to create for a variety of reasons. The only real reason for doing this as opposed to #1 might be performance (it's potentially more 'native').
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

Bill,
Please don't cross post the same question in multiple forums. This question is already posted in JDBC


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: returning # of rows from sql query
 
Similar Threads
regarding MS access sql query
SQL commands in MS-Access....Pls Help
SQL Server limiting result set
returning # of rows from sql query
dynamic pagging