jdbc 5.0

1.事务

  事务将单个SQL语句或一组SQL语句视为一个逻辑单元,如果任何语句失败,整个事务将失败。

  jdbc的MySQL驱动程序中的事务默认是自动提交。

  默认情况下,每个SQL语句在完成后都会提交到数据库。

2.事务自动提交模式的关闭与开启

  调用Connection对象的setAutoCommit(false/true)方法。

3.提交与回滚

 1 package com.rong.jielong;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.SQLException;
 7 import java.sql.Timestamp;
 8 import java.text.SimpleDateFormat;
 9 import java.util.Date;
10 import java.util.Properties;
11 
12 public class Test9 {
13 
14     /**
15      * MySQL默认事务自动提交 jdbc事务处理
16      * 
17      * @author 容杰龙
18      */
19     public static void main(String[] args) {
20         Connection conn = null;
21         try {
22             Class.forName("com.mysql.jdbc.Driver");
23             String url = "jdbc:mysql://127.0.0.1:3306/rjl";
24             Properties info = new Properties();
25             info.setProperty("user", "rjl");
26             info.setProperty("password", "123");
27             conn = DriverManager.getConnection(url, info);
28             // 设置关闭事务自动提交
29             conn.setAutoCommit(false);
30             String sql = "insert into time(timestamp) values(?)";
31             // 格式化时间对象
32             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
33             // 将字符串格式化为Date时间对象
34             Date date = sdf.parse("1995-09-24 13:14:03");
35 
36             PreparedStatement ps = conn.prepareStatement(sql);
37             ps.setTimestamp(1, new Timestamp(date.getTime()));
38             int count = ps.executeUpdate();
39             if (count > 0) {
40                 System.out.println("成功!");
41             } else {
42                 System.out.println("失败!");
43             }
44             // 事务提交======若不提交,则不能更新数据库的数据
45             conn.commit();
46         } catch (Exception e) {
47             try {
48                 // 事务包含错误执行语句,执行回滚
49                 conn.rollback();
50                 System.out.println("事务有错!回滚!添加失败!");
51             } catch (SQLException e1) {
52                 e1.printStackTrace();
53             }
54         }
55 
56     }
57 
58 }

4.保存点

  设置保存点(Savepoint)时,可以在事务中定义逻辑回滚点。

  使用Connection对象的方法:

  • setSavepoint(String savepointName): - 定义新的保存点,它还返回一个Savepoint对象。
  • releaseSavepoint(Savepoint savepointName): - 删除保存点。要注意,它需要一个Savepoint对象作为参数。 该对象通常是由setSavepoint()方法生成的保存点。
  • 有一个rollback (Savepoint savepointName)方法,它将使用事务回滚到指定的保存点。

 1 package com.rong.jielong;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.SQLException;
 7 import java.sql.Savepoint;
 8 import java.sql.Timestamp;
 9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 import java.util.Properties;
12 
13 public class Test10 {
14 
15     /**
16      * 事务保存点
17      * @author 容杰龙
18      */
19     public static void main(String[] args) {
20         Connection conn = null;
21         Savepoint savepoint=null;
22         try {
23             Class.forName("com.mysql.jdbc.Driver");
24             String url = "jdbc:mysql://127.0.0.1:3306/rjl";
25             Properties info = new Properties();
26             info.setProperty("user", "rjl");
27             info.setProperty("password", "123");
28             conn = DriverManager.getConnection(url, info);
29             // 设置关闭事务自动提交
30             conn.setAutoCommit(false);
31             String sql = "insert into time(timestamp) values(?)";
32             // 格式化时间对象
33             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
34             // 将字符串格式化为Date时间对象
35             Date date = sdf.parse("1995-09-24 13:14:03");
36             //执行语句一
37             PreparedStatement prepareStatement = conn.prepareStatement(sql);
38             prepareStatement.setTimestamp(1, new Timestamp(new Date().getTime()));
39             prepareStatement.executeUpdate();
40             //设置保存点
41             savepoint = conn.setSavepoint("savePoint");
42 
43             //执行语句二
44             PreparedStatement ps = conn.prepareStatement(sql);
45             ps.setTimestamp(1, new Timestamp(date.getTime()));
46             ps.executeUpdate();
47             
48             // 事务提交======若不提交,则不能更新数据库的数据
49             conn.commit();
50         } catch (Exception e) {
51             try {
52                 // 若事务包含错误执行语句,回滚到特定的保存点
53                 conn.rollback(savepoint);
54                 System.out.println("事务有错!回滚!添加失败!");
55             } catch (SQLException e1) {
56                 e1.printStackTrace();
57             }
58         }finally{
59             if (conn!=null) {
60                 try {
61                     // 事务最终提交,若保存点之前语句无误,则可提交保存点之前的操作
62                     conn.commit();
63                     conn.close();
64                 } catch (SQLException e) {
65                     e.printStackTrace();
66                 }
67                 
68             }
69         }
70 
71     }
72 
73 }
原文地址:https://www.cnblogs.com/57rongjielong/p/7787033.html