• 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

Rowcount in Resultset

Mouly Kumaraswamy
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I find the number of rows returned by a SQL query without iterating through all the rows? One way would be to add rowcount column in the select clause and order it - but this is very expensive and clumsy.

Also can is it possible to identify upfront if zero rows have been returned?
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't tell how many rows are in a ResultSet or if it is empty. I created my own utility class to do such things. However, it is easy to do the same. The idea would be to put the rows of the ResultSet into an ArrayList. I use that approach and right before returning i convert it to an Object[][] which for most of my needs is more convenient. There is a discussion of this issue here http://forum.java.sun.com/thread.jspa?threadID=484092&messageID=2262448.

I extracted my post and pasted it below.

<<The open source FormattedDataSet has a ResultSetConverter object that converts a ResultSet to an Object[][]. From this you could easily put the array in a collection or just use the array directly.


See the following sites for more info including javadocs, and a live demo.

http://www.fdsapi.com - The easiest way to generate dynamic XML and HTML
http://www.jamonapi.com - A performance tuning API>>
Paper jam tastes about as you would expect. Try some on this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
    Bookmark Topic Watch Topic
  • New Topic