How to get length/size of ResultSet in Java?


Finding size of ResultSet in Java becomes bit tricky because ResultSet does not offer any size(), length() method or any other relevant method to find size of ResultSet in java.


In this tutorial I’ll be posting two ways to find size/length/NumberOfRows in ResultSet object in java.


1st program/example to find size/length/NumberOfRows in ResultSet object in java>

     int rowCount = 0;
     while (resultSet.next()) {
          rowCount++;
     }
     System.out.println("Total number of rows in ResultSet object = "+rowCount);
    
Simply iterate on ResultSet object and increment rowCount to obtain size of ResultSet object in java.


2nd way to find size/length/NumberOfRows in ResultSet object in java>

--Before executing java program execute these database scripts  >
create table EMPLOYEE(ID number(4),NAME varchar2(22));
insert into EMPLOYEE values(1, 'ankit');
insert into EMPLOYEE values(2, 'rohit');
insert into EMPLOYEE values(3, 'amy');
commit;


Example/Programs JDBC- find size/length/NumberOfRows in ResultSet object in java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class FindResultSetSizeExample {
public static void main(String... arg) {
     Connection con = null;
     PreparedStatement prepStmt = null;
     ResultSet rs = null;             
     try {
          // registering Oracle driver class
          Class.forName("oracle.jdbc.driver.OracleDriver");
          // getting connection
          con = DriverManager.getConnection(
                   "jdbc:oracle:thin:@localhost:1521:orcl",
                   "ankit", "Oracle123");
         
          prepStmt = con.prepareStatement("select NAME from EMPLOYEE",
                   ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
         
          rs = prepStmt.executeQuery();
         

          int rowCount = 0;
          if (rs.last()) {//make cursor to point to the last row in the ResultSet object
           rowCount = rs.getRow();
           rs.beforeFirst(); //make cursor to point to the front of the ResultSet object, just before the first row.
          }
          System.out.println("Total number of rows in ResultSet object = "+rowCount);
         

          System.out.println("\nNow, lets display all records in ResultSet object");
          while (rs.next()) {
              System.out.println(rs.getString("NAME"));
          }
         
         
     } catch (ClassNotFoundException e) {
          e.printStackTrace();
     } catch (SQLException e) {
          e.printStackTrace();
     }
     finally{
          try {
              if(rs!=null) rs.close(); //close resultSet
              if(prepStmt!=null) prepStmt.close(); //close PreparedStatement
              if(con!=null) con.close(); // close connection
          } catch (SQLException e) {
              e.printStackTrace();
          }
     }
}
}
/*OUTPUT
Total number of rows in ResultSet object = 3
Now, lets display all records in ResultSet object
ankit
rohit
amy
*/

Note : If rs.last() returns false that means size of ResultSet object is 0.

In this tutorial we learned how to calculate size/length/NumberOfRows in ResultSet object in java.


Related >

JDBC - What is ResultSet in java - Types, concurrency, holdability of ResultSet in java

Jdbc - ResultSetMetaData in java - Retrieve table column name and datatype


JDBC - DatabaseMetaData in java - retrieve database information



Must read for you :
Labels:

No comments:

Post a Comment