JDBC事务处理

Posted 天再高,踮起脚尖就能更接近阳光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC事务处理相关的知识,希望对你有一定的参考价值。

直接上下代码:

 1 package com.learn.jdbc.chap09;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.SQLException;
 6 
 7 import com.learn.jdbc.util.DbUtil;
 8 
 9 /**
10  * JDBC事务处理-----转账实例
11  * @author Administrator
12  *
13  */
14 public class Demo1 {
15     private static DbUtil dbUtil=new DbUtil();
16     
17     /**
18      * 转出
19      * @param con
20      * @param accountName
21      * @param amount
22      * @throws Exception
23      */
24     private static void outAccount(Connection con,String accountName,Double amount) throws Exception{
25         String sql="update sp_account set accountBalance=accountBalance-? where accountName=?";
26         PreparedStatement pstmt = con.prepareStatement(sql);
27         pstmt.setDouble(1, amount);
28         pstmt.setString(2, accountName);
29         pstmt.executeUpdate();
30         
31     }
32     
33     /**
34      * 转入
35      * @param con
36      * @param accountName
37      * @param amount
38      * @throws Exception
39      */
40     private static void inAccount(Connection con,String accountName,Double amount) throws Exception{
41         String sql="update sp_account set accountBalance=accountBalance+? where accountName=?";
42         PreparedStatement pstmt = con.prepareStatement(sql);
43         pstmt.setDouble(1, amount);
44         pstmt.setString(2, accountName);
45         pstmt.executeUpdate();
46         
47     }
48     
49     public static void main(String[] args) {
50         Connection con=null;
51         
52         try {
53             con = dbUtil.getCon();
54             System.out.println("张三向李四转账");
55             Double amount=500.00;
56             outAccount(con,"张三",amount);
57             inAccount(con,"李四",amount);
58             System.out.println("转帐成功!");
59         } catch (Exception e) {
60             // TODO Auto-generated catch block
61             e.printStackTrace();
62         }finally{
63             try {
64                 con.close();
65             } catch (SQLException e) {
66                 // TODO Auto-generated catch block
67                 e.printStackTrace();
68             }
69         }
70         
71         
72         
73         
74         
75         
76     }
77 }
 1 package com.learn.jdbc.chap09;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.SQLException;
 6 
 7 import com.learn.jdbc.util.DbUtil;
 8 
 9 /**
10  * JDBC事务处理---转账失败,使用事务机制进行回滚
11  * @author Administrator
12  *
13  */
14 public class Demo2 {
15     private static DbUtil dbUtil=new DbUtil();
16     
17     /**
18      * 转出
19      * @param con
20      * @param accountName
21      * @param amount
22      * @throws Exception
23      */
24     private static void outAccount(Connection con,String accountName,Double amount) throws Exception{
25         String sql="update sp_account set accountBalance=accountBalance-? where accountName=?";
26         PreparedStatement pstmt = con.prepareStatement(sql);
27         pstmt.setDouble(1, amount);
28         pstmt.setString(2, accountName);
29         pstmt.executeUpdate();
30         
31     }
32     
33     /**
34      * 转入
35      * @param con
36      * @param accountName
37      * @param amount
38      * @throws Exception
39      */
40     private static void inAccount(Connection con,String accountName,Double amount) throws Exception{
41         String sql="update sp_account set accountBalance1=accountBalance+? where accountName=?";
42         PreparedStatement pstmt = con.prepareStatement(sql);
43         pstmt.setDouble(1, amount);
44         pstmt.setString(2, accountName);
45         pstmt.executeUpdate();
46         
47     }
48     
49     public static void main(String[] args) {
50         Connection con=null;
51         
52         try {
53             con = dbUtil.getCon();
54             con.setAutoCommit(false);  // 1. 取消自动提交--事务开始
55             System.out.println("张三向李四转账");
56             Double amount=500.00;
57             outAccount(con,"张三",amount);
58             inAccount(con,"李四",amount);
59             System.out.println("转帐成功!");
60         } catch (Exception e) {
61             // TODO Auto-generated catch block
62             try {
63                 con.rollback(); // 2. 回滚--事务回滚
64                 System.out.println("事务回滚!");
65             } catch (SQLException e1) {
66                 // TODO Auto-generated catch block
67                 e1.printStackTrace();
68             }
69             e.printStackTrace();
70         }finally{
71             try {
72                 con.commit(); // 3. 提交事务---事务结束
73                 System.out.println("提交事务!"); // 注意:mysql的MyISAM模式不支持事务机制,要用事务,就用InnoDB
74                 con.close();
75             } catch (SQLException e) {
76                 // TODO Auto-generated catch block
77                 e.printStackTrace();
78             }
79         }
80         
81         
82         
83         
84         
85         
86     }
87 }
 1 package com.learn.jdbc.chap09;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.SQLException;
 6 import java.sql.Savepoint;
 7 
 8 import com.learn.jdbc.util.DbUtil;
 9 
10 /**
11  * JDBC事务处理---转账失败,使用事务机制进行回滚--设置事务保存点(注意:工作中不怎么使用保存点)
12  * @author Administrator
13  *
14  */
15 public class Demo3 {
16     private static DbUtil dbUtil=new DbUtil();
17     
18     /**
19      * 转出
20      * @param con
21      * @param accountName
22      * @param amount
23      * @throws Exception
24      */
25     private static void outAccount(Connection con,String accountName,Double amount) throws Exception{
26         String sql="update sp_account set accountBalance=accountBalance-? where accountName=?";
27         PreparedStatement pstmt = con.prepareStatement(sql);
28         pstmt.setDouble(1, amount);
29         pstmt.setString(2, accountName);
30         pstmt.executeUpdate();
31         
32     }
33     
34     /**
35      * 转入
36      * @param con
37      * @param accountName
38      * @param amount
39      * @throws Exception
40      */
41     private static void inAccount(Connection con,String accountName,Double amount) throws Exception{
42         String sql="update sp_account set accountBalance1=accountBalance+? where accountName=?";
43         PreparedStatement pstmt = con.prepareStatement(sql);
44         pstmt.setDouble(1, amount);
45         pstmt.setString(2, accountName);
46         pstmt.executeUpdate();
47         
48     }
49     
50     public static void main(String[] args) {
51         Connection con=null;
52         Savepoint  sp=null;
53         
54         try {
55             con = dbUtil.getCon();
56             con.setAutoCommit(false);  // 1. 取消自动提交--事务开始
57             System.out.println("张三向李四转账");
58             Double amount=500.00;
59             outAccount(con,"张三",amount);
60             sp=con.setSavepoint();    // 2. 设置一个保存点
61             inAccount(con,"李四",amount);
62             System.out.println("转帐成功!");
63         } catch (Exception e) {
64             // TODO Auto-generated catch block
65             try {
66                 con.rollback(sp); // 3. 回滚--事务回滚到保存点
67                 System.out.println("事务回滚!");
68             } catch (SQLException e1) {
69                 // TODO Auto-generated catch block
70                 e1.printStackTrace();
71             }
72             e.printStackTrace();
73         }finally{
74             try {
75                 con.commit(); // 4. 提交事务---事务结束
76                 System.out.println("提交事务!"); // 注意:mysql的MyISAM模式不支持事务机制,要用事务,就用InnoDB
77                 con.close();
78             } catch (SQLException e) {
79                 // TODO Auto-generated catch block
80                 e.printStackTrace();
81             }
82         }
83         
84         
85         
86         
87         
88         
89     }
90 }

 

以上是关于JDBC事务处理的主要内容,如果未能解决你的问题,请参考以下文章

JDBC 事务

Java JDBC程序中对于数据库事务的处理详解

spring03-SpringJdbcTemplate模板技术和事务处理

在 Tomcat7 中使用 JDBC 连接池进行事务处理

JDBC事务管理

JDBC事务处理&批量处理