Using savepoints with the JDBC-ODBC Bridge: Unsupp

2019-09-07 23:13发布

问题:

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