JAVA学习笔记(五十四)- 事务Transaction与批处理

事务Transaction


/*
 * 事务Transaction
 * JDBC默认开启自动提交事务,需要关闭,才能手动控制事务
 * 
 * 默认MySQL存储引擎为My...,其不支持事务处理,修改为InnoDB引擎
 * create table 表名(  ) engine=InnoDB default charset=utf8;
 * 
 * InnoDB引擎默认关闭自动事务提交,修改MySQL为自动提交事务
 * set autocommit=1;  //1表示自动提交,0表示关闭自动提交
 * show variables like ‘autocommit‘;
 */
public class Test01 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        conn = DBUtil.getConnection();
        try {
            conn.setAutoCommit(false);//关闭自动提交事务,手动控制事务
            String sql = "insert into user values (null,‘eee‘,‘123‘)";
            pstmt = conn.prepareStatement(sql);
            pstmt.executeUpdate(); //插入第一个用户

            String sql2="insert into user values (‘fff‘,‘123‘)";
            pstmt=conn.prepareStatement(sql2);
            pstmt.executeUpdate();//插入第二个用户
            conn.commit();//提交事务
            System.out.println("插入用户成功!");
        } catch (SQLException e) {
            System.out.println("出现异常,回滚事务");
            try {
                conn.rollback();//回滚事务
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            DBUtil.closeAll(rs, pstmt, conn);
        }
    }
}

批处理



/*
 * 批处理
 */
public class Test02 {
    Connection conn = null;
    Statement stmt = null;
    PreparedStatement pstmt=null;
    ResultSet rs = null;

    public void test01() {
        try {
            conn = DBUtil.getConnection();
            stmt = conn.createStatement();
            // 添加批处理语句
            stmt.addBatch("insert into user values (null,‘s001‘,‘123‘)");
            stmt.addBatch("insert into user values (null,‘s002‘,‘123‘)");
            stmt.addBatch("insert into user values (null,‘s003‘,‘123‘)");
            // 执行批处理
            int[] nums = stmt.executeBatch();
            System.out.println(Arrays.toString(nums));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs, stmt, conn);
        }
    }

    public void test02() {
        try {
            conn = DBUtil.getConnection();
            conn.setAutoCommit(false);
            pstmt=conn.prepareStatement("insert into user values (null,?,?)");
            // 添加批处理语句
            pstmt.setObject(1, "t001");
            pstmt.setObject(2, "123");
            pstmt.addBatch();
            pstmt.setObject(1, "t002");
            pstmt.setObject(2, "123");
            pstmt.addBatch();
            pstmt.setObject(1, "t003");
            pstmt.setObject(2, "123");
            pstmt.addBatch();
            // 执行批处理
            int[] nums=pstmt.executeBatch();
            System.out.println(Arrays.toString(nums));
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.closeAll(rs, stmt, conn);
        }
    }

    public static void main(String[] args) {
        Test02 test = new Test02();
        test.test02();
    }
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。