JDBC 事物Transaction

//Test

 1 import com.zhongxy.utils.JdbcUtils;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 
 8 public class TestTransaction {
 9     public static void main(String[] args) {
10         Connection conn = null;
11         PreparedStatement st = null;
12         ResultSet rs = null;
13         try {
14             conn = JdbcUtils.getConnection();
15             //关闭自动提交  也就是开启事物
16             conn.setAutoCommit(false);
17 
18             String sql1 = "update users set price = price + 100 where id = 1";
19             st = conn.prepareStatement(sql1);
20             st.executeUpdate();
21 
22             String sql2 = "update users set price = price - 200 where id = 1";
23             st = conn.prepareStatement(sql2);
24             st.executeUpdate();
25 
26             conn.commit();//事物完毕,提交
27             System.out.println("执行成功");
28         } catch (SQLException e) {
29             try {
30                 conn.rollback();
31             } catch (SQLException e1) {
32                 e1.printStackTrace();
33             }
34             e.printStackTrace();
35         }finally {
36             JdbcUtils.release(conn,st,rs);
37         }
38     }
39 }

//Utils

 1 public class JdbcUtils {
 2     private static String driver = null;
 3     private static String url = null;
 4     private static String username = null;
 5     private static String password = null;
 6     static {
 7         try{
 8             InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
 9             Properties properties = new Properties();
10             properties.load(in);
11             driver = properties.getProperty("driver");
12             url = properties.getProperty("url");
13             username = properties.getProperty("username");
14             password = properties.getProperty("password");
15 
16             //1.加载驱动
17             Class.forName(driver);
18         }catch (Exception e){
19             e.printStackTrace();
20         }
21         
22     }
23     //获取连接
24     public static Connection getConnection() throws SQLException {
25          return DriverManager.getConnection(url,username,password);
26     }
27     //释放资源
28     public  static void release(Connection conn, Statement st, ResultSet rs){
29         if(rs!=null){
30             try{
31                 rs.close();
32             } catch (SQLException e) {
33                 e.printStackTrace();
34             }
35         }
36         if(st!=null){
37             try {
38                 st.close();
39             } catch (SQLException e) {
40                 e.printStackTrace();
41             }
42         }
43         if(conn!=null){
44             try {
45                 conn.close();
46             } catch (SQLException e) {
47                 e.printStackTrace();
48             }
49         }
50 
51     }
52 
53 }

 

posted @ 2022-10-14 18:29  西东怪  阅读(10)  评论(0编辑  收藏  举报
返回顶端