0
public class JDBC {

static Connection con;
static Statement stmt;

public static void main(String argv[]){

connect();
 con.setAutoCommit(false);

     // Statement s=  conn.createStatement();

       con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);

        //set a Savepoint


        System.out.println("savepoint1");
        String SQL = "INSERT INTO Flight VALUES (1000, '22/7/2013', 'LGW', 'MAN', 10,40)";
        stmt.executeUpdate(SQL);

        Savepoint savepoint1 = conn.setSavepoint("Savepoint1");


        String SQL2 = "UPDATE Flight SET flightid = 500 WHERE Flightid = 1000";
        stmt.executeUpdate(SQL2);

        con.rollback(savepoint1);


        con.commit();
        System.out.println("end");
}



static void connect() throws SQLException, ClassNotFoundException, DataSourceException {
    try {
        // load database driver driver

        Class.forName(DataSource.getClassName());

        // connect to database from a given URL with a given username and
        // password

        Connection con = DriverManager.getConnection(DataSource.getURL(), DataSource.getUserName(), DataSource.getPassword());

        // create an SQL statement object
        stmt = con.createStatement();
    } catch (SQLException se) {
        con = null;
        stmt = null;
        throw (se);
    }
    }       
   }
}

你好,我想了解如何使用保存点。但是每次我运行程序时..它都会出现空指针异常,表示错误发生在 con.setautocommit(false);

任何建议将不胜感激......

4

1 回答 1

1

很可能,您没有正确初始化Connection con变量。您没有显示那部分代码,因此我们无法为您提供更多帮助。

于 2013-04-18T04:01:55.327 回答