1 Import Java. SQL .* ; 2 Public Class Testtransaction { 3 4 5 Public Static Void Main (string [] ARGs ){ 6 7 Connection conn =Null ; 8 Statement stmt = Null ; 9 10 Try { 11 Class. forname ("oracle. JDBC. Driver. oracledriver" ); 12 Conn = drivermanager. getconnection ("JDBC: oracle: thin: @ 127.0.0.1: 1521: sxt", "Scott", "Tiger" ); 13 // Disable autocommit that is enabled by default. 14 Conn. setautocommit ( False ); 15 Stmt = Conn. createstatement ();
// Disable the implementation operation 16 Stmt. addbatch ("insert into dept2 values (51, '20140901', 'hahaha ')" ); 17 Stmt. addbatch ("insert into dept2 values (52, '20140901', 'hahaha ')" ); 18 Stmt. addbatch ("insert into dept2 values (53, '20140901', 'hahaha ')"); 19 Stmt.exe cutebatch (); 20 Conn. Commit ();
// Restore to true after execution
21 Conn. setautocommit ( True ); 22 } Catch (Classnotfoundexception e ){ 23 E. printstacktrace (); 24 }Catch (Sqlexception e ){ 25 26 E. printstacktrace (); 27 28 Try { 29 If (Conn! = Null ) 30 { 31 Conn. rollback (); 32 Conn. setautocommit ( True ); 33 } 34 } Catch (Sqlexception E1 ){ 35 E1.printstacktrace (); 36 } 37 }Finally { 38 Try { 39 If (Stmt! = Null ) 40 Stmt. Close (); 41 If (Conn! = Null ) 42 Conn. Close (); 43 } Catch (Sqlexception e ){ 44 E. printstacktrace (); 45 } 46 } 47 48 49 } 50 51 }