JDBC closes database connection and autocommit "Go"

Source: Internet
Author: User

 //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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.