首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从CachedRowSet连接

从CachedRowSet连接
EN

Stack Overflow用户
提问于 2014-11-24 03:42:06
回答 1查看 1.2K关注 0票数 1

我有下面的Java7代码来创建一个CachedRowSet

代码语言:javascript
复制
CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet(); 

有办法从CachedRowSet对象获取连接对象吗?在调用acceptChanges() on CachedRowSet之前,我希望在连接对象上将CachedRowSet设置为false,因为在调用acceptChanges()时将得到以下异常。

代码语言:javascript
复制
javax.sql.rowset.spi.SyncProviderException: Can't call commit when autocommit=true

COMMIT_ON_ACCEPT_CHANGES上有一个CachedRowSet字段,但不推荐它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-24 08:45:11

嗯,我花了一段时间才把这件事讲到最后。通过autoCommitConnectionConnection值设置为false解决了这个问题。

以下是示例工作程序:

代码语言:javascript
复制
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;
      }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27097537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档