Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Rowcount in Resultset

 
Mouly Kumaraswamy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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
  • 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>>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic