wood burning stoves*
The moose likes JDBC and the fly likes How to find the size of my resultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to find the size of my resultSet" Watch "How to find the size of my resultSet" New topic
Author

How to find the size of my resultSet

sree lekha
Greenhorn

Joined: Jun 30, 2006
Posts: 7
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

Joined: Feb 04, 2006
Posts: 96
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

Joined: Apr 20, 2006
Posts: 3344

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().


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Shantanu Puranik
Ranch Hand

Joined: Jun 10, 2004
Posts: 67
Hello Sree,

If you want the count of rows in ResultSet,



--- Shantanu
Suresh patel
Ranch Hand

Joined: Sep 13, 2007
Posts: 32
Thats perfect !!!
Steven Kuypers
Greenhorn

Joined: Nov 23, 2010
Posts: 1
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

Joined: Aug 22, 2010
Posts: 3610
    
  60

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

Joined: Nov 03, 2005
Posts: 135
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.
 
GeeCON Prague 2014
 
subject: How to find the size of my resultSet