Getting the Number of Rows in a Database Table - Java JDBC

Java examples for JDBC:Table

Description

Getting the Number of Rows in a Database Table

Demo Code

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

public class Main {
  public static void main(String[] args) {
    try {/*from w ww.j  a v  a2s .c o m*/
      Connection connection = null;
      // Select the number of rows in the table
      Statement stmt = connection.createStatement();
      ResultSet resultSet = stmt.executeQuery("SELECT COUNT(*) FROM my_table");

      // Get the number of rows from the result set
      resultSet.next();
      int rowcount = resultSet.getInt(1);
    } catch (SQLException e) {
    }
  }
}

Related Tutorials