• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

total number of rows in ResultSet

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
reply
    Bookmark Topic Watch Topic
  • New Topic