This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes returning # of rows from sql query Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes 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: 26184
    
  66

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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: returning # of rows from sql query
 
Similar Threads
returning # of rows from sql query
SQL Server limiting result set
SQL commands in MS-Access....Pls Help
regarding MS access sql query
dynamic pagging