一个用JDBC处理transaction的样例程序import java.sql.*;public class TestJDBC { public static void main(String [] args) {
一个用JDBC处理transaction的样例程序
import java.sql.*;
public class TestJDBC {
public static void main(String [] args) {
Connection cOnn= null;
Statement stmt = null;
try {
Class.forName("Oracle.jdbc.driver.OracleDriver");
cOnn= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","scott","tiger");
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.addBatch("insert into dept2 values (61,'test','apple')");
stmt.addBatch("insert into dept2 values (62,'test','apple')");
stmt.addBatch("insert into dept2 values (63,'test','apple')");
stmt.executeBatch();
conn.commit();
conn.setAutoCommit(true);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
if(conn!=null) {
try {
conn.rollback();
conn.setAutoCommit(true);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}
finally {
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}