Getting the Number of Rows in a Bidirectional Scrollable ResultSet - Java JDBC

Java examples for JDBC:ResultSet

Description

Getting the Number of Rows in a Bidirectional Scrollable ResultSet

Demo Code

import static java.sql.ResultSet.CONCUR_READ_ONLY;
import static java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE;

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

public class Main {
  public static void main(String[] args) {
    Connection conn = null;/*from w w  w. ja  va2  s  . com*/
    Statement stmt = null;

    try {
      conn = JDBCUtil.getConnection();
      stmt = conn.createStatement(TYPE_SCROLL_INSENSITIVE, CONCUR_READ_ONLY);
      String SQL = "select person_id, first_name, last_name, dob, "
          + "income from person";
      ResultSet rs = stmt.executeQuery(SQL);
      int cursorType = rs.getType();
      if (cursorType == ResultSet.TYPE_FORWARD_ONLY) {
        System.out
            .println("JDBC driver returned a " + "forward - only cursor.");
      } else {
        rs.last();
        int rowCount = rs.getRow();
        System.out.println("Row Count: " + rowCount);
        rs.beforeFirst();
      }
      while (rs.next()) {
        System.out.println("Person ID: " + rs.getInt(1));
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      JDBCUtil.closeStatement(stmt);
      JDBCUtil.commit(conn);
      JDBCUtil.closeConnection(conn);
    }
  }
}

class JDBCUtil {
  public static Connection getConnection() throws SQLException {
    Driver derbyEmbeddedDriver = null;// new
                                      // org.apache.derby.jdbc.EmbeddedDriver();
    DriverManager.registerDriver(derbyEmbeddedDriver);
    String dbURL = "jdbc:derby:beginningJavaDB;create=true;";
    String userId = "root";
    String password = "password";

    Connection conn = DriverManager.getConnection(dbURL, userId, password);
    conn.setAutoCommit(false);
    return conn;
  }

  public static void closeConnection(Connection conn) {
    try {
      if (conn != null) {
        conn.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void closeStatement(Statement stmt) {
    try {
      if (stmt != null) {
        stmt.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void closeResultSet(ResultSet rs) {
    try {
      if (rs != null) {
        rs.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void commit(Connection conn) {
    try {
      if (conn != null) {
        conn.commit();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void rollback(Connection conn) {
    try {
      if (conn != null) {
        conn.rollback();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    Connection conn = null;
    try {
      conn = getConnection();
      System.out.println("Connetced to the database.");
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      closeConnection(conn);
    }
  }
}

Related Tutorials