Getting the Number of Rows in a Table Using a Scrollable Result Set - Java JDBC

Java examples for JDBC:ResultSet

Description

Getting the Number of Rows in a Table Using a Scrollable Result Set

Demo Code

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {
  public static void main(String[] args) throws Exception {
    Connection connection = null;
    // Create a scrollable result set
    Statement stmt = connection.createStatement(
        ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Move to the end of the result set
    resultSet.last();// ww w  .  jav  a  2  s  . com

    // Get the row number of the last row which is also the row count
    int rowCount = resultSet.getRow();
  }
}

Related Tutorials