How to Determine the Size of a java.sql.ResultSet in Java?

Question

How can I find out the size of a java.sql.ResultSet in Java?

// Example of working with ResultSet in Java
import java.sql.*;

public class ResultSetSizeExample {
  public static void main(String[] args) throws SQLException {
    Connection connection = DriverManager.getConnection("jdbc:your_database", "user", "password");
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("SELECT * FROM your_table");

    // Count the number of rows
    int rowCount = 0;
    while (resultSet.next()) {
      rowCount++;
    }
    resultSet.beforeFirst(); // Reset cursor to the beginning

    System.out.println("Row count: " + rowCount);
    
    resultSet.close();
    statement.close();
    connection.close();
  }
}

Answer

In Java, the `java.sql.ResultSet` does not provide a direct method to retrieve its size, such as `size()` or `length()`. Instead, measuring the size of a ResultSet typically involves iterating through its contents to count the number of rows it contains. Here’s how to do this effectively:

// Example of counting rows using scrolling ResultSet
import java.sql.*;

public class ResultSetRowCountExample {
  public static void main(String[] args) throws SQLException {
    Connection connection = DriverManager.getConnection("jdbc:your_database", "user", "password");
    Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = statement.executeQuery("SELECT * FROM your_table");

    if (resultSet.last()) { // Move cursor to the last row
      int rowCount = resultSet.getRow(); // Get current row index
      System.out.println("Row count: " + rowCount);
    }

    resultSet.close();
    statement.close();
    connection.close();
  }
}

Causes

  • Lack of direct methods in ResultSet for size calculation.
  • ResultSet is designed to retrieve data incrementally, not all at once.

Solutions

  • Iterate through the ResultSet to count rows
  • Use `resultSet.last()` and `resultSet.getRow()` to get the row count (if the result is scrollable)
  • Store query results into a collection that can be sized easily

Common Mistakes

Mistake: Forgetting to set the ResultSet type to TYPE_SCROLL_INSENSITIVE.

Solution: Ensure to create the Statement with appropriate ResultSet type to allow scrolling.

Mistake: Assuming getRow() always works with non-scrollable ResultSets.

Solution: Note that getRow() only gives valid results for scrollable ResultSets.

Helpers

  • java.sql.ResultSet size
  • how to count ResultSet rows in Java
  • Java JDBC ResultSet row count

Related Questions

⦿How to Configure Maven to Execute All Tests Even When Some Fail

Learn how to configure Maven to run all tests across modules even if some tests fail in earlier modules. Discover the solution stepbystep.

⦿How to Log a Formatted Message and Exception Stack Trace Using SLF4J?

Learn how to log a formatted message with an object array and an exception stack trace using SLF4J.

⦿How to Import a .CER Certificate into a Java Keystore for Web Service Authentication?

Learn the steps to import a .cer certificate into a Java keystore for web service authentication including troubleshooting tips and common mistakes.

⦿How to Check if a Double is NaN in Java?

Learn how to effectively verify if a double value is NaN in Java with clear explanations and examples.

⦿Understanding the 'continue' Keyword in Java: Usage and Functionality

Learn about the continue keyword in Java its functionality and when to use it effectively in your coding practices.

⦿How to Convert JSON to a HashMap using Gson

Learn how to easily convert JSON responses to a HashMap in Java using the Gson library and access nested data structures.

⦿How to Safely Stop a Thread in Java Without Exceptions?

Learn how to properly stop a thread in Java without throwing exceptions using a wellstructured approach.

⦿Understanding Static Initialization Blocks in Java

Learn about static initialization blocks in Java their purpose and how they enhance code organization for static fields.

⦿How to Use IntelliJ's Organize Imports Feature for Java

Learn how to automatically organize imports in IntelliJ IDEA for Java files similar to Eclipses feature.

⦿How to Resolve the "chromedriver" Cannot Be Opened Error on macOS Catalina (10.15.3)

Learn how to fix the chromedriver cannot be opened because the developer cannot be verified error on macOS Catalina 10.15.3 while using Selenium.

© Copyright 2025 - CodingTechRoom.com

close