Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Why no count of rows in ResultSetMetaData?

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Craig
[ February 18, 2003: Message edited by: Craig Demyanovich ]
 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Craig Demyanovich
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siarhei,
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.
Craig
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic