JAVA learning notes ()-Transaction and batch processing, javatransaction
Transaction
/** Transaction * JDBC Enables automatic Transaction commit by default. You need to disable the Transaction to manually control the Transaction. ** by default, the MySQL storage engine is My ..., it does not support transaction processing. It is changed to InnoDB engine * create table Name () engine = InnoDB default charset = utf8; ** the InnoDB engine disables automatic transaction commit by default, modify MySQL to automatically submit a transaction * set autocommit = 1; // 1 indicates that the transaction is automatically committed, and 0 indicates that the automatic commit is disabled * show variables like 'autocommit '; */public class Test01 {public static void main (String [] args) {Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = n Ull; conn = DBUtil. getConnection (); try {conn. setAutoCommit (false); // close the automatic commit transaction and manually control the transaction String SQL = "insert into user values (null, 'Eee ', '000000')"; pstmt = conn. prepareStatement (SQL); pstmt.exe cuteUpdate (); // insert the first user String sql2 = "insert into user values ('fff', '000000')"; pstmt = conn. prepareStatement (sql2); pstmt.exe cuteUpdate (); // Insert the second user conn. commit (); // submit the transaction System. out. println ("User inserted successfully! ");} Catch (SQLException e) {System. out. println ("exception occurred, rollback transaction"); try {conn. rollback (); // rollback transaction} catch (SQLException e1) {e1.printStackTrace () ;}} finally {DBUtil. closeAll (rs, pstmt, conn );}}}
Batch Processing
/** Batch Processing */public class Test02 {Connection conn = null; Statement stmt = null; PreparedStatement pstmt = null; ResultSet rs = null; public void test01 () {try {conn = DBUtil. getConnection (); stmt = conn. createStatement (); // Add a batch processing statement stmt. addBatch ("insert into user values (null, 's001', '000000')"); stmt. addBatch ("insert into user values (null, 's002 ', '000000')"); stmt. addBatch ("insert into user values (null, 'S003 ', '000000') "); // execute the batch processing int [] nums = stmt.exe cuteBatch (); System. out. println (Arrays. toString (nums);} catch (SQLException e) {e. printStackTrace ();} finally {DBUtil. closeAll (rs, stmt, conn) ;}} public void test02 () {try {conn = DBUtil. getConnection (); conn. setAutoCommit (false); pstmt = conn. prepareStatement ("insert into user values (null ,?,?) "); // Add the batch processing statement pstmt. setObject (1, "t001"); pstmt. setObject (2, "123"); pstmt. addBatch (); pstmt. setObject (1, "t002"); pstmt. setObject (2, "123"); pstmt. addBatch (); pstmt. setObject (1, "t003"); pstmt. setObject (2, "123"); pstmt. addBatch (); // execute batch processing int [] nums?pstmt.exe cuteBatch (); System. out. println (Arrays. toString (nums); conn. commit ();} catch (SQLException e) {try {conn. rollback ();} catch (SQLException e1) {e1.printStackTrace ();} e. printStackTrace ();} finally {DBUtil. closeAll (rs, stmt, conn) ;}} public static void main (String [] args) {Test02 test = new Test02 (); test. test02 ();}}