Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to find the size of my resultSet

 
Greenhorn
Posts: 7
  • 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
 
Ranch Hand
Posts: 96
  • 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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • 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().
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sree,

If you want the count of rows in ResultSet,

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats perfect !!!
 
Greenhorn
Posts: 1
  • 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 }
}



 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 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.
 
Ranch Hand
Posts: 135
  • Likes 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.
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic