标签:
1 package com.xxyh.jdbc; 2 import java.sql.Connection; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 public class TxTest { 7 public static void main(String[] args) { 8 Connection conn = null; 9 Statement stmt = null; 10 ResultSet rs = null; 11 try { 12 conn = JdbcUtils.getConnection(); 13 stmt = conn.createStatement(); 14 String sql = "update user set money=money-10 where id=1"; 15 stmt.executeUpdate(sql); 16 17 sql = "select money from user where id=2"; 18 rs = stmt.executeQuery(sql); 19 float money = 0.0f; 20 if (rs.next()) { 21 money = rs.getFloat("money"); 22 } 23 if (money > 300) 24 throw new RuntimeException("已经超过最大值!"); 25 sql = "update user set money=money+10 where id=2"; 26 stmt.executeUpdate(sql); 27 } catch (SQLException e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 } finally{ 31 JdbcUtils.close(rs, stmt, conn); 32 } 33 } 34 }
1 package com.xxyh.jdbc; 2 import java.sql.Connection; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 public class TxTest { 7 public static void main(String[] args) throws SQLException { 8 Connection conn = null; 9 Statement stmt = null; 10 ResultSet rs = null; 11 try { 12 conn = JdbcUtils.getConnection(); 13 conn.setAutoCommit(false);// 打开事务,关闭自动提交 14 stmt = conn.createStatement(); 15 String sql = "update user set money=money-10 where id=1"; 16 stmt.executeUpdate(sql); 17 18 sql = "select money from user where id=2"; 19 rs = stmt.executeQuery(sql); 20 float money = 0.0f; 21 if (rs.next()) { 22 money = rs.getFloat("money"); 23 } 24 if (money > 300) 25 throw new RuntimeException("已经超过最大值!"); 26 sql = "update user set money=money+10 where id=2"; 27 stmt.executeUpdate(sql); 28 conn.commit(); 29 } catch (SQLException e) { 30 if (conn != null) 31 conn.rollback(); 32 throw e; 33 } finally{ 34 JdbcUtils.close(rs, stmt, conn); 35 } 36 } 37 }
1 package com.xxyh.jdbc; 2 import java.sql.Connection; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.sql.Savepoint; 6 import java.sql.Statement; 7 public class SavePointTest { 8 public static void main(String[] args) throws SQLException { 9 Connection conn = null; 10 Statement stmt = null; 11 ResultSet rs = null; 12 Savepoint sp = null; 13 try { 14 conn = JdbcUtils.getConnection(); 15 conn.setAutoCommit(false);// 打开事务,关闭自动提交 16 stmt = conn.createStatement(); 17 String sql = "update user set money=money-10 where id=1"; 18 stmt.executeUpdate(sql); 19 20 sp = conn.setSavepoint(); 21 22 // 转账的同时更新id=3的账户余额 23 sql = "update user set money=money+10 where id=3"; 24 stmt.executeUpdate(sql); 25 26 sql = "select money from user where id=2"; 27 rs = stmt.executeQuery(sql); 28 float money = 0.0f; 29 if (rs.next()) { 30 money = rs.getFloat("money"); 31 } 32 if (money > 200) 33 throw new RuntimeException("已经超过最大值!"); 34 sql = "update user set money=money+10 where id=2"; 35 stmt.executeUpdate(sql); 36 conn.commit(); 37 } catch(RuntimeException e) { 38 if (conn != null && sp != null) { 39 conn.rollback(sp);// 回滚到保存点 40 conn.commit(); 41 } 42 throw e; 43 } catch (SQLException e) { 44 if (conn != null) 45 conn.rollback(); 46 throw e; 47 } finally{ 48 JdbcUtils.close(rs, stmt, conn); 49 } 50 } 51 }
标签:
原文地址:http://www.cnblogs.com/xiaoxiaoyihan/p/4372756.html