Using savepoints with the JDBC-ODBC Bridge: UnsupportedOperationException

落爺英雄遲暮 提交于 2019-12-13 04:48:50

问题


I have connected NetBeans IDE with MS Access and while doing a transaction I got this error. It seems that savepoints are not supported...Please guide me..

             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("jdbc:odbc:cse");
            Statement stmt1, stmt2, stmt3;
                     System.out.println("Statements created");
            conn.setAutoCommit(false);
            String query1 = " update registration set id='105' " + 
                            "where first = 'Sumit' ";
            String query2 = " update registration set id='106' " + 
                            "where first = 'Zayed' ";
             System.out.println(" Queries created");


            stmt1 = conn.createStatement();
            System.out.println(" Connection created");
            Savepoint s1 = conn.setSavepoint("sp1");
             System.out.println(" Savept created");

            stmt2 = conn.createStatement();

            stmt1.executeUpdate(query1);
            stmt2.executeUpdate(query2);

            conn.commit();

            stmt3 = conn.createStatement();

            stmt1.close();
            stmt2.close();
            conn.releaseSavepoint(s1);
            conn.close();

The error is Statements created Queries created Connection created Error: java.lang.UnsupportedOperationException


回答1:


The JDBC-ODBC Bridge apparently does not support Savepoints at all. However, the UCanAccess JDBC driver does support unnamed Savepoints:

String connStr = "jdbc:ucanaccess://C:/__tmp/test.mdb";
try (Connection conn = DriverManager.getConnection(connStr)) {
    conn.setAutoCommit(false);
    try (Statement s = conn.createStatement()) {
        s.executeUpdate("UPDATE ucaTest SET Field2='NEWVALUE1' WHERE ID=1");
    }
    java.sql.Savepoint sp1 = conn.setSavepoint();
    try (Statement s = conn.createStatement()) {
        s.executeUpdate("UPDATE ucaTest SET Field2='NEWVALUE2' WHERE ID=2");
    }
    conn.rollback(sp1);
    conn.commit();
} catch (Exception e) {
    e.printStackTrace(System.out);
}

For more information on using UCanAccess see

Manipulating an Access database from Java without ODBC



来源:https://stackoverflow.com/questions/26837855/using-savepoints-with-the-jdbc-odbc-bridge-unsupportedoperationexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!