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');

--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 */
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
                 // getting connection
                 con = DriverManager.getConnection(
                              "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.updateString("NAME", "AnkitUpdated"); //java.sql.SQLException: Invalid operation for read only resultset: updateString
          } catch (ClassNotFoundException e) {
          } catch (SQLException e) {
                 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) {
Connection established successfully!
java.sql.SQLException: Invalid operation for read only resultset: updateString
   at oracle.jdbc.driver.SQLStateMapping.newSQLException(
   at oracle.jdbc.driver.DatabaseError.newSQLException(
   at oracle.jdbc.driver.DatabaseError.throwSqlException(
   at oracle.jdbc.driver.DatabaseError.throwSqlException(
   at oracle.jdbc.driver.DatabaseError.throwSqlException(
   at oracle.jdbc.driver.BaseResultSet.updateString(
   at oracle.jdbc.driver.OracleResultSet.updateString(
   at CONCUR_READ_ONLY_ResultSetConcurrency.main(

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
//start paginationJs //end paginationJs