Java -- JDBC 学习--事务
                        
                            时间:2021-07-01 10:21:17
                            帮助过:11人阅读
							                        
                     
                    
                    
                     void testTransaction() {
        Connection connection = 
null;
        try {
            connection =
 JDBCTools.getConnection();
            System.out.println(connection.getAutoCommit());
            // 开始事务: 取消默认提交.
            connection.setAutoCommit(
false);
            String sql = "UPDATE users SET balance = "
                    + "balance - 500 WHERE id = 1"
;
            update(connection, sql);
            int i = 10 / 0
;
            System.out.println(i);
            sql = "UPDATE users SET balance = " + "balance + 500 WHERE id = 2"
;
            update(connection, sql);
            // 提交事务
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            // 回滚事务
            try {
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            JDBCTools.releaseDB(null, 
null, connection);
        }
 
Java -- JDBC 学习--事务
标签:date   use   管理   jdb   语句   string   多个   code   logs