File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases 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 and Relational Databases
Bookmark "How to find the size of my resultSet" Watch "How to find the size of my resultSet" New topic

How to find the size of my resultSet

sree lekha

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

krishnamoorthy kitcha
Ranch Hand

Joined: Feb 04, 2006
Posts: 96
Hi Sree

Using the ResultSet rs ;

while {
//it moves the query affected
if i.e is affected the row


not affected check and tell me your feedback

Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Originally posted by sree lekha:
..i need to find if my resultSet has got records or not.


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

Joined: Nov 23, 2010
Posts: 1

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

Joined: Aug 22, 2010
Posts: 3733

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


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.
I agree. Here's the link:
subject: How to find the size of my resultSet
It's not a secret anymore!