Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

total number of rows in ResultSet

 
Nehul NN
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to find total number of rows in ResultSet? Do we have any method which says you retrieve 100 rows after executing xyz sql statement!! or you do resultset.next() in while loop to count row number in that resultset...
 
Mathias Nilsson
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry! There is no method to retrive the rows in a resultset. Only if
you have deleted,updated or inserted rows.
You can do something like this
Using SQL Query:

Using JDBC Scrollable ResultSet: Can be very time counsuming if the resultset is big

You can altso use a cached rowset.

// Hope this helps
[ November 08, 2003: Message edited by: Mathias Nilsson ]
 
Nehul NN
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I did those trick.. I am looking for this method since 1997 in JDBC API.. wonder what will be big problem...only reason I can think of they have to scroll to entire resultset before it is available to us, when you fire select * from table;
SCJP 1.4, SCWCD & Preparing for SCJD, SCBCD
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that ResultSet is designed so that it's possible for an implementation to start returning rows as soon as it finds them, even though the query is still running on the server. So at the time you first start processing a ResultSet, the information about how many rows there will be total is not necessarily available - and if you asked for it, you would force the system to block until all the rows had been returned, which could be rather inefficient. Instead JDBC is set up so that if you really want the row count right away, you need to write a query that returns the row count, specifically.
 
Nehul NN
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but don't you think by firing one more query i.e. select count(*) from table you are introducing more overhead or additional time for processing data from database. Instead of that resultset can setup variable which will set rowcount of resultset after retrieving entire resultset.
Hope I am not asking something new... :roll:
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35266
383
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nehul,
It definitely adds more overhead because you would basically be doing the query twice. I think the point people are trying to make is that the count(*) query is more efficient if you only need the number of rows. If you need any other data, you should stick with one query.
 
Ram Kumar Subramaniam
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
I think that ResultSet is designed so that it's possible for an implementation to start returning rows as soon as it finds them, even though the query is still running on the server. So at the time you first start processing a ResultSet, the information about how many rows there will be total is not necessarily available - and if you asked for it, you would force the system to block until all the rows had been returned, which could be rather inefficient. Instead JDBC is set up so that if you really want the row count right away, you need to write a query that returns the row count, specifically.

But Jim it would depend on the vendor implementation of the same right ? Or is it that the latest spec enforces this implementation ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic