Program to use TYPE_SCROLL_INSENSITIVE ResultSet type in java



Before reading this article I’ll recommend you to read : JDBC - What is ResultSet in java - Types, concurrency, holdability of ResultSet in java

Program to demonstrate how to use TYPE_SCROLL_INSENSITIVE ResultSet type 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;

--If table already exists then execute the DROP command >
drop table EMPLOYEE;


EMPLOYEE table will look like this >

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 TYPE_SCROLL_INSENSITIVE_ResultSetType {
   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");
                 System.out.println("Connection established successfully!");
                
                 //ResultSet type = TYPE_SCROLL_INSENSITIVE
                 prepStmt = con.prepareStatement("select NAME from EMPLOYEE",
                            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
                
                 System.out.println("\nLet's display all records >");
                 rs = prepStmt.executeQuery();
                 while (rs.next()) {
                       System.out.println(rs.getString("NAME"));
                 }
                
                 System.out.println("\nTYPE_SCROLL_INSENSITIVE - "+rs.getType()); //1004 is TYPE_SCROLL_INSENSITIVE
                
                 System.out.println("\nLet's move cursor to first position in ResultSet object");
                 rs.first();
                 System.out.println(rs.getString("NAME"));
                 System.out.println("\nLet's move cursor to last position in ResultSet object");
                 rs.last();
                 System.out.println(rs.getString("NAME"));
                
                 System.out.println("\nLet's move cursor to previous position in ResultSet object");
                 rs.previous();
                 System.out.println(rs.getString("NAME"));
                
                 System.out.println("\nLet's move cursor to third row in ResultSet object > using absolute");
                 rs.absolute(3);
                 System.out.println(rs.getString("NAME"));
                
                 System.out.println("\nLet's move cursor from third to first row in ResultSet object > using relative > (3rd row - 2 = 1st row)");
                 rs.relative(-2);
                 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
Connection established successfully!
Let's display all records >
ankit
rohit
amy
TYPE_SCROLL_INSENSITIVE - 1004
Let's move cursor to first position in ResultSet object
ankit
Let's move cursor to last position in ResultSet object
amy
Let's move cursor to previous position in ResultSet object
rohit
Let's move cursor to third row in ResultSet object > using absolute
amy
Let's move cursor from third to first row in ResultSet object > using relative > (3rd row - 2 = 1st row)
ankit
*/

Labels: Core Java JDBC
eEdit
Must read for you :