File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to find the size of my resultSet

 
sree lekha
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i need to find if my resultSet has got records or not.

resultSet = statement.executeQuery(query);

how do i find the size of my resultSet after the query is executed.

Please help me out.

Regards
Sreelekha
 
krishnamoorthy kitcha
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sree

Using the ResultSet rs ;

while rs.next() {
//it moves the query affected
}
if rs.next() i.e is affected the row

else

not affected check and tell me your feedback

Rgks
k.krishnamoorthy.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sree lekha:
..i need to find if my resultSet has got records or not.


Sreelekha,

As krishnamoorthy said, you can check the next() method of resultset object which gets you inside if the resultset stores some rows fetched out of the query execution.

The resultset object works in a way that it has got a pointer before the very first row of the results fetched. Whenever you want to start iterating (looping it over), you have to invoke the next() on the resultset object, which intern increments the pointer to point to the next row. For the first time, it would point to the very first-and-beginning row.

Once all the rows are iterated (processed) (means,when the pointer is in last row), the next() method returns false as there are NO more rows to process.


..how do i find the size of my resultSet after the query is executed.


What exactly do you mean the size of resultset? Did you mean to ask the number of rows or columns(count) the resultset contains? If that case, there is no direct method provided for the same. But indirectly you can use something called ResultSetMetaData which has a method getColumnCount().
 
Shantanu Puranik
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sree,

If you want the count of rows in ResultSet,

 
Suresh patel
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats perfect !!!
 
Steven Kuypers
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

the code beneath is suitable; and it will not change the poistion of the cursor

if (rs != null) {
if (rs.isBeforeFirst()) {
// true if the cursor is before the first row; false if the cursor is at any other position or the result set contains no rows }
}



 
Martin Vajsar
Sheriff
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to count the records is to process them and count the number of processed records. If you do not actually need to process the records, it is much better for performance to formulate the SQL query so that it returns just the count and nothing else.

Java has excellent classes, eg. ArrayList, that will grow to accommodate all data you need. You do not have to bother to get the size first, allocate the array and then fill it. Not only the code would be unnecessarily more complex if you do, but, depending on your database and/or driver, the performance might actually be a lot worse than starting with the default-sized ArrayList and having it grow.
 
Adam Richards
Ranch Hand
Posts: 135
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not clear what your objective is. If you're simply trying to see if there are ANY rows meeting a certain condition, you could do this directly in SQL, something like

SELECT NULL FROM table_name
WHERE EXISTS (.....)

This will return a row if they exist, no rows if not. If you want to PROCESS the rows, then you'll need to select some columns instead of just NULL.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic