Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
    Bookmark Topic Watch Topic
  • New Topic

returning # of rows from sql query

 
Bill Brasskey
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
Bill,
Please don't cross post the same question in multiple forums. This question is already posted in JDBC
 
    Bookmark Topic Watch Topic
  • New Topic