当前位置:Gxlcms > mysql > 一个用JDBC处理transaction的样例程序

一个用JDBC处理transaction的样例程序

时间:2021-07-01 10:21:17 帮助过:29人阅读

一个用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();
}
}
}
}
}

linux

人气教程排行