Catalogue
1. Reuse methods in Connection
2 JDBC Transaction Management Classic Case
Review of common methods in 1 Connection classes
1.1 Statement createStatement() throws SQLException;
Create a Statement instance (that is, create an SQL execution object)
1.2 PreparedStatement prepareStatement(String sql) throws SQLException;
Create a PreparedStatement object (that is, create a precompiled SQL execution object)
1.3 void setAutoCommit(boolean autoCommit) throws SQLException;
Set up automatic commit of transactions (false is to turn off automatic commit, true is to start automatic commit)
1.4 void commit() throws SQLException;
Manual Submission of Transactions
1.5 void rollback() throws SQLException;
Manual rollback of transactions
2. The classic case of transaction rollback: bank transfer case
Turn-out and turn-in are transactions. If the turn-out succeeds but fails, transaction rollbacks will be required. Otherwise, the turn-out balance will decrease but the turn-in balance will not increase.
Note: Transaction submission and rollback are invoked by the method provided by Connection; transactions are essentially dependent on the implementation of the database; and the method of Connection is essentially the invocation of the database transaction mechanism.
2.1 Transfer business without transaction control
Disadvantage: If the transfer succeeds, but the transfer fails, the transfer balance will decrease, but the transfer balance will remain unchanged.
Project structure chart
Transfer business java source code1 package cn.xiangxu.entity; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.util.Scanner; 6 7 import cn.xiangxu.tools.DBUtil; 8 9 public class Test { 10 public static void main(String[] args) { 11 Scanner scanner = new Scanner(System.in); 12 System.out.println("Please enter a roll-out username:"); 13 String outName = scanner.nextLine(); 14 System.out.println("Please enter the amount of funds to be transferred:"); 15 Double money = Double.parseDouble(scanner.nextLine()); 16 System.out.println("Please enter the input username:"); 17 String inName = scanner.nextLine(); 18 System.out.println("The transfer account is:" + outName + "The amount transferred is:" + money + "Transferred to the account is:" + inName); 19 20 21 Connection conn = null; 22 try { 23 conn = DBUtil.getConnection(); // Instantiate connection objects 24 25 // conn.setAutoCommit(false); // Turn off automatic commit transaction function 26 27 String sql = "UPDATE client " 28 + "SET account = account - ? " 29 + "WHERE name = ? "; 30 PreparedStatement ps = conn.prepareStatement(sql); 31 ps.setDouble(1, money); 32 ps.setString(2, outName); 33 Integer rs = ps.executeUpdate(); 34 if(rs > 0) { 35 System.out.println("Turn Out Success"); 36 } else { 37 System.out.println("Turn Out Failure"); 38 return; // The following statement is no longer executed; however, the following statement is not executed. finally The statement in the sentence will still be executed, because even if the sky falls down finally Statements in the 39 } 40 41 System.out.println("======Partition line======="); 42 43 String sql_in = "UPDATE client " 44 + "SET account = account + ? " 45 + "WHERE name = ? "; 46 PreparedStatement ps_in = conn.prepareStatement(sql_in); 47 ps_in.setDouble(1, money); 48 ps_in.setString(2, inName); 49 Integer judge_in = ps_in.executeUpdate(); 50 if(judge_in > 0) { 51 System.out.println("Turn to Success"); 52 // conn.commit(); // Turn-in and roll-out are successful in committing transactions 53 } else { 54 System.out.println("Turn into failure"); 55 // conn.rollback(); // Roll back the transaction if the roll-out is successful or the roll-in is unsuccessful 56 } 57 58 // conn.setAutoCommit(true); // Open Autocommit Transaction 59 60 } catch (Exception e) { 61 // TODO Auto-generated catch block 62 e.printStackTrace(); 63 } finally { 64 System.out.println("I am finally The statement in"); 65 try { 66 DBUtil.closeConnection(); 67 } catch (Exception e) { 68 // TODO Auto-generated catch block 69 e.printStackTrace(); 70 } 71 } 72 } 73 }