/*
* 事务提交
*/
@Test
public void test5() {
Connection conn = null;
PreparedStatement statement = null;
ResultSet reSet = null;
try {
// 获取连接对象
conn = jdbcUtil.getConnection();
//设置手动执行 true 自动 false 手动
conn.setAutoCommit(false);
// 查询sql语句
String sql1 = "UPDATE student SET NAME='王五' WHERE id=5; ";
String sql2 = "UPDATE student SET NAME='王五' WHERE id=6; ";
//执行存储过程
statement=conn.prepareStatement(sql1);
statement.executeUpdate();
//
statement=conn.prepareStatement(sql2);
statement.executeUpdate();
} catch (Exception e) {
//回滚事务
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
throw new RuntimeException(e);
}
}finally{
//提交事务
try {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
* 事务提交
*/
@Test
public void test5() {
Connection conn = null;
PreparedStatement statement = null;
ResultSet reSet = null;
try {
// 获取连接对象
conn = jdbcUtil.getConnection();
//设置手动执行 true 自动 false 手动
conn.setAutoCommit(false);
// 查询sql语句
String sql1 = "UPDATE student SET NAME='王五' WHERE id=5; ";
String sql2 = "UPDATE student SET NAME='王五' WHERE id=6; ";
//执行存储过程
statement=conn.prepareStatement(sql1);
statement.executeUpdate();
//
statement=conn.prepareStatement(sql2);
statement.executeUpdate();
} catch (Exception e) {
//回滚事务
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
throw new RuntimeException(e);
}
}finally{
//提交事务
try {
conn.commit();
jdbcUtil.close(conn, statement, reSet);
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
}