One-point record of the Java JDBC database: Transaction
Import Java. SQL. *; public class testbatch {public static void main (string [] ARGs) throws sqlexception {connection conn = NULL; statement stmt = NULL; preparedstatement pstmt = NULL; try {class. forname ("com. mySQL. JDBC. driver "); Conn = drivermanager. getconnection ("JDBC: mysql: // localhost/test", "root", "Zhen"); Conn. setautocommit (false); // transaction 1 transactionstmt = Conn. createstatement (); // execute a static SQL statement and return Object. Stmt. addbatch ("insert into student values (30, 'ddd ', 12, 'II')"); stmt. addbatch ("insert into student values (31, 'ddd ', 12, 'II')"); stmt. addbatch ("insert into student values (32, 'ddd ', 12, 'II')"); stmt. addbatch ("insert into student values (33, 'ddd ', 12, 'II')" Wait until stmt.exe cutebatch (); // transaction 2 pstmt = Conn. preparestatement ("insert into student values (?,?,?,?) "); Pstmt. setint (1, 40); pstmt. setstring (2, "QQ"); pstmt. setint (3, 5); pstmt. setstring (4, "ww"); pstmt. addbatch (); pstmt. setint (1, 41); pstmt. setstring (2, "QQ"); pstmt. setint (3, 5); pstmt. setstring (4, "ww" using fig (); // Conn. commit (); // If transaction 1 and transaction 2 must be completed at the same time or not at the same time, and only one of them cannot be completed, use transaction} catch (classnotfoundexception E1) {e1.printstacktrace ();} catch (sqlexception e) {e. printstacktrace (); Conn. rollback (); // if the transaction processing is lost, the conn must be rolled back. setautocommit (true);} finally {stmt. close (); pstmt. close (); Conn. close ();}}}