Program to use CONCUR_READ_ONLY ResultSet concurrency is used 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 show updating ResultSet will throw java.sql.Exception when   CONCUR_READ_ONLY ResultSet concurrency is used in java
CONCUR_READ_ONLY -  CONCUR_READ_ONLY  mode means ResultSet object is read only it may not be updated.
--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 CONCUR_READ_ONLY_ResultSetConcurrency {
   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 concurrency = CONCUR_READ_ONLY
                 prepStmt = con.prepareStatement("select NAME from EMPLOYEE",
                             ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
                
                 rs = prepStmt.executeQuery();
                
                 System.out.println("CONCUR_READ_ONLY - "+rs.getConcurrency()); //1007 is CONCUR_READ_ONLY
                
                 rs.next();
                 System.out.println(rs.getString("NAME"));
                 rs.updateString("NAME", "AnkitUpdated"); //java.sql.SQLException: Invalid operation for read only resultset: updateString
                 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!
CONCUR_READ_ONLY - 1007
ankit
java.sql.SQLException: Invalid operation for read only resultset: updateString
   at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
   at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
   at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
   at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:263)
   at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:271)
   at oracle.jdbc.driver.BaseResultSet.updateString(BaseResultSet.java:302)
   at oracle.jdbc.driver.OracleResultSet.updateString(OracleResultSet.java:819)
   at CONCUR_READ_ONLY_ResultSetConcurrency.main(CONCUR_READ_ONLY_ResultSetConcurrency.java:37)
*/

No comments:

Post a Comment