简体   繁体   中英

Connection from CachedRowSet

I have the following Java 7 code to create a CachedRowSet .

CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet(); 

Is there a way to get the Connection object from the CachedRowSet object? I would like to set autoCommit to false on the Connection object before invoking the acceptChanges() on CachedRowSet as I'm getting the following exception when invoking acceptChanges() .

javax.sql.rowset.spi.SyncProviderException: Can't call commit when autocommit=true

There is a COMMIT_ON_ACCEPT_CHANGES field on CachedRowSet , but it's Deprecated.

Well, it took some time for me to reproduce the issue at my end. Setting the autoCommit value of the Connection to false via conn.setAutoCommit(false); resolved this issue.

Following is the sample working program:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetProvider;

public class CRSetChecker {    
    public static void main(String[] args) {    
        String connectString = "jdbc:oracle:thin:scott/tiger" + 
                "@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)" + 
                "(HOST=myorahost)(PORT=5521))" + 
                "(CONNECT_DATA=(SID=myorasid)))";

        //Get DB connection
        Connection conn = (new CRSet()).getConnection(connectString);
        if (conn == null) {
            System.out.println("Connection failed");

            System.exit(0);
        } else {
            System.out.println("Connection established successfully!");

            try {
                CachedRowSet crs = 
                              RowSetProvider.newFactory().createCachedRowSet(); 
                String query="select ename from emp";
                crs.setCommand(query);
                crs.execute(conn);
                //Set auto commit false
                conn.setAutoCommit(false);
                int count = 0;
                while(crs.next()){
                    String name = crs.getString(1);
                    count++;                        
                    System.out.println(name);
                    if(count==1){                       
                        crs.updateString(1, "COOPER");
                        crs.updateRow();
                        crs.acceptChanges(conn);
                        System.out.println("After update:"+crs.getString(1));
                    }
                }
                conn.close();
            } catch (SQLException e1) {
                e1.printStackTrace();
            } 
        }
    }

    public Connection getConnection(String connectString)
      {
        Connection con = null;
        try {
          try {
            Class.forName("oracle.jdbc.OracleDriver");
          } catch (ClassNotFoundException e) {
            e.printStackTrace();
          }

          con = DriverManager.getConnection(connectString);
        } catch (SQLException ex) {
          ex.printStackTrace();
        }

        return con;
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM