JDBC 關閉資料庫連接與自動認可【轉】

來源:互聯網
上載者:User

標籤:final   val   關閉自動   pwd   mit   out   false   bsp   nal   

 

 

 // Jdbc關閉資料庫連接時,會隱含一個提交事務的操作                        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 String DB_NAME = "scott";        private final static String DB_PWd = "scott";                     //jdbc關閉自動認可. 在對資料庫的操作未提交時,當conn.close()時,會預設先提交事務,再關閉串連.        public static 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); //關閉自動認可                ps = conn.prepareStatement("insert into t_user values (?, ?, ?)");                ps.setString(1, "1");                ps.setString(2, "1");                ps.setString(3, "1");                int results = ps.executeUpdate();                //conn.commit();//不手動提交                System.out.println("插入了" + results + "條: ");                 } 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關閉自動認可. 在jdbc未提交時,當conn.close()時,會預設先提交事務,再關閉串連.                    } catch (SQLException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }            }        }                        //jdbc預設自動認可. 在對資料庫的操作未手動提交時,也會立即提交到資料庫並生效        public static 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預設自動認可                ps = conn.prepareStatement("insert into t_user values (?, ?, ?)");                ps.setString(1, "1");                ps.setString(2, "1");                ps.setString(3, "1");                int results = ps.executeUpdate();  //注意,這一步操作就被提交到資料庫了!!!                //conn.commit();//不手動提交                System.out.println("插入了" + results + "條: ");                 } 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關閉串連                    } catch (SQLException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }            }        }

 

完全引用自

JDBC 關閉資料庫連接與自動認可--53193361

 

JDBC 關閉資料庫連接與自動認可【轉】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.