当前位置:Gxlcms > 数据库问题 > JDBC事务处理

JDBC事务处理

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

@Test 2 public void testTransaction() throws Exception { 3 Connection connection = null; 4 try{ 5 connection = jdbcTools.getConnection(); 6 System.out.println(connection); 7 connection.setAutoCommit(false); 8 String sql = "update user set balance = balance - 500 where id = 1"; 9 update(connection,sql); 10 11 sql = "update user set balance = balance + 500 where id = 2"; 12 update(connection,sql); 13 14 connection.commit(); 15 16 }catch (Exception e){ 17 e.printStackTrace(); 18 connection.rollback(); 19 }finally { 20 jdbcTools.releaseResource(null,null,connection); 21 } 22 23 24 } 25 26 public void update (Connection connection,String sql){ 27 PreparedStatement preparedStatement = null; 28 try { 29 preparedStatement = connection.prepareStatement(sql); 30 preparedStatement.execute(); 31 } catch (Exception e) { 32 e.printStackTrace(); 33 }finally { 34 jdbcTools.releaseResource(null,preparedStatement,null); 35 } 36 }

 

JDBC事务处理

标签:

人气教程排行