//when JDBC closes a database connection, it implies a commit transaction operation private final static String Db_driver="Oracle.jdbc.driver.OracleDriver"; Private final static String db_connection="Jdbc:oracle:thin:@127.0.0.1:1521: Mydb01 "; Private final static Stringdb_name ="Scott"; Private final static String Db_pwd="Scott"; //JDBC turns off auto-commit. When the operation on the database is not committed, when Conn.Close(), the transaction is committed by default, and then the connection is closed. Publicstatic void Test1 () {Connection conn= NULL; CallableStatement callstmt= NULL; PreparedStatement PS= NULL; try {class.forname (db_driver); Conn=Drivermanager.getconnection (Db_connection,db_name, DB_PWD); Conn.setautocommit (FALSE); //turn off Auto-submit PS=Conn.preparestatement ("Insert intoT_userValues (?, ?, ?)"); Ps.setstring (1, "1"); Ps.setstring (2, "1"); Ps.setstring (3, "1"); intResults=ps.executeupdate (); //Conn.Commit();//do not manually commit System.out.println ("inserted"+Results+"article:"); } catch (Exception e) {try {conn.rollback(); } catch (SQLException E1) {//TODO Auto-generated catch block E1.printstacktrace (); } e.printstacktrace (System.out); } finally {try {conn.setautocommit (true); } catch (SQLException e) {//TODO Auto-generated catch block E.printstacktrace (); } if(NULL !=callstmt) {try {callstmt.Close(); } catch (SQLException e) {//TODO Auto-generated catch block E.printstacktrace (); } } if(NULL !=conn) {try {conn.Close();//JDBC turns off auto-commit. When JDBC is not committed, when Conn.Close(), the transaction is committed by default, and then the connection is closed. } catch (SQLException e) {//TODO Auto-generated catch block E.printstacktrace (); } } } } //JDBC is automatically committed by default. When the operation on the database is not manually committed, it is also immediately committed to the database and takes effect Publicstatic void Test2 () {Connection conn= NULL; CallableStatement callstmt= NULL; PreparedStatement PS= NULL; try {class.forname (db_driver); Conn=Drivermanager.getconnection (Db_connection,db_name, DB_PWD); //Conn.setautocommit (FALSE);//JDBC Default auto-commit PS=Conn.preparestatement ("Insert intoT_userValues (?, ?, ?)"); Ps.setstring (1, "1"); Ps.setstring (2, "1"); Ps.setstring (3, "1"); intResults=Ps.executeupdate ();//Note that this step is committed to the database!!! //Conn.Commit();//do not manually commit System.out.println ("inserted"+Results+"article:"); } catch (Exception e) {try {conn.rollback(); } catch (SQLException E1) {//TODO Auto-generated catch block E1.printstacktrace (); } e.printstacktrace (System.out); } finally {try {conn.setautocommit (true); } catch (SQLException e) {//TODO Auto-generated catch block E.printstacktrace (); } if(NULL !=callstmt) {try {callstmt.Close(); } catch (SQLException e) {//TODO Auto-generated catch block E.printstacktrace (); } } if(NULL !=conn) {try {conn.Close();//jdbc Close connection} catch (SQLException e) {//TODO Auto-generated catch block E.printstacktrace (); } } } }
Fully referenced from
JDBC closes database connection and autocommit--53193361
JDBC closes database connection and autocommit "Go"