File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and the fly likes Why no count of rows in ResultSetMetaData? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Why no count of rows in ResultSetMetaData?" Watch "Why no count of rows in ResultSetMetaData?" New topic

Why no count of rows in ResultSetMetaData?

Craig Demyanovich
Ranch Hand

Joined: Sep 25, 2000
Posts: 173
Hello everyone,
Let me say that I know how to obtain the row count of a ResultSet. It's been covered many times in this forum, and Sun provides an answer on the JDBC FAQ page.
Though I formulated this question a very long time ago, I've never asked: Why doesn't ResultSetMetaData provide a row count?
Do databases provide such metadata about the results of a query? If not, why not? Why must we incur the expense (even when it is minimal) of iterating the ResultSet, either via many calls to next() or via last() and getRow(), to obtain information that is more often than not extremely valuable to the consumer of the ResultSet? Finally, can we expect that such an API will be available in a future version of JDBC?
As I said earlier, I know how to obtain the row count of a ResultSet. I don't have any problems to solve, merely an old curiosity that I hope to satisfy. Feel free to spend your time on posts whose purpose is to solve problems.
Thanks for your time,
[ February 18, 2003: Message edited by: Craig Demyanovich ]
Serge Adzinets
Ranch Hand

Joined: Nov 26, 2002
Posts: 166
Actually, JDBC driver knows nothing about how many rows the query has returned. It has only the cursor for the result set. It gets every row from the database via that cursor every time you call next() method.
It seems to be logical not to store the whole result set in memory, isn't it?
That's how I see it.

Best Regards,<br />Serge
Craig Demyanovich
Ranch Hand

Joined: Sep 25, 2000
Posts: 173
I agree that caching all rows in the ResultSet is dangerous in that resources could be quickly consumed for queries that return a very large number of rows. ResultSet would be inflexible and difficult to use if we always needed to consider the size of the results before issuing a query.
Say I issue a query whose result contains 1000 rows. ResultSet is implemented such that I can use it as though all 1000 rows are contained in it. This design makes ResultSet easy to use; the caching is an implementation detail that I don't need to know in order to use ResultSet effectively. I don't want to change this behavior of ResultSet.
What I would like is that ResultSetMetaData can tell me how many rows are in the result, i.e., how many rows matched my query. Since we consider a ResultSet to hold all of our result, whether it does or not, the count of rows would be a very valuable piece of metadata. (Consider how many posts there are for how to obtain the count of rows in a ResultSet!) If ResultSetMetaData provided it, we would not need to write extra code to obtain it. Not needing to write this extra code simplifies our code, enhances the usability of the API and eliminates the many questions that pertain to row count issues.
I agree. Here's the link:
subject: Why no count of rows in ResultSetMetaData?