JDBC事务操作(转账案例)和使用Utils改进

未使用事务

/*
    JDBC完成转账功能
 */
public class Demo01Account {
    public static void main(String[] args) throws SQLException {
        //1. 获取连接Connection对象
        Connection con = C3P0Util.getConnection();
        //2. Connection对象获取执行sql语句的Statement对象
        Statement stmt = con.createStatement();
        //3. 定义2条sql语句
        String tomSql = "update account set money = money - 1000 where name = 'tom'";
        String jerrySql = "update account set money = money + 1000 where name = 'jerry'";
        //4. Statement对象调方法执行sql语句,获取结果
        int tomRes = stmt.executeUpdate(tomSql);
        //5. 处理结果
        if (tomRes > 0){
            System.out.println("tom扣款1000元成功");
        }else {
            System.out.println("tom扣款1000元失败");
        }

        int jerryRes = stmt.executeUpdate(jerrySql);
        if (jerryRes > 0){
            System.out.println("jerry收款1000元成功");
        }else {
            System.out.println("jerry收款1000元失败");
        }
        //6. 关闭资源
        C3P0Util.release(con,stmt,null);
    }
}
View Code

在未使用事务时,默认使用mysql事务处理方式,此时如果出现异常,转账将会出现问题。

那么就需要我们使用事务,将两条sql语句必须要么都成功,要么都失败。

JDBC使用事务

public class Demo02Account {
    public static void main(String[] args) {
        //提升变量作用域
        Connection con = null;
        Statement stmt = null;
        try {
            //1. 获取连接Connection对象
            con = C3P0Util.getConnection();
            //2. Connection对象开启事务
            con.setAutoCommit(false);
            //3. Connection对象获取执行sql语句的Statement对象
            stmt = con.createStatement();
            //4. 定义2条sql语句
            String tomSql = "update account set money = money - 1000 where name = 'tom'";
            String jerrySql = "update account set money = money + 1000 where name = 'jerry'";
            //5. Statement对象调方法执行sql语句,获取结果
            int tomRes = stmt.executeUpdate(tomSql);

            int jerryRes = stmt.executeUpdate(jerrySql);
            //7. 处理结果
            if (tomRes > 0){
                System.out.println("tom扣款1000元成功");
            }else {
                System.out.println("tom扣款1000元失败");
            }
            if (jerryRes > 0){
                System.out.println("jerry收款1000元成功");
            }else {
                System.out.println("jerry收款1000元失败");
            }
            //6. 如果sql语句正常执行,没有出现问题,提交事务
            con.commit();
        } catch (Exception throwables) {
            throwables.printStackTrace();
            //8. 如果sql语句执行过程中出现问题,回滚事务
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            //9. 关闭资源
            C3P0Util.release(con,stmt,null);
        }
    }
}
View Code

 

手动创建异常(此时回滚事务,两条sql语句都不会执行)

使用c3p0连接池时,需要的代码量很多,那么此时就可以使用Utils简化JDBC开发

public class Demo03Account {
    public static void main(String[] args) {
        Connection con = null;
        try {
            //1. 空参构造创建QueryRunner对象
            QueryRunner qr = new QueryRunner();
            //2. 获取连接对象
            con = C3P0Util.getConnection();
            //3. 连接对象开启事务
            con.setAutoCommit(false);
            //定义sql语句
            String tomSql = "update account set money = money - ? where name = ?";
            String jerrySql = "update account set money = money + ? where name = ?";
            //4. QueryRunner对象执行sql语句获取结果
            int tomRes = qr.update(con, tomSql, 1000, "tom");
            int jerryRes = qr.update(con, jerrySql, 1000, "jerry");
            //5. sql语句正常执行,提交事务,处理结果
            con.commit();
            if (tomRes > 0){
                System.out.println("tom扣款1000元成功");
            }else {
                System.out.println("tom扣款1000元失败");
            }
            if (jerryRes > 0){
                System.out.println("jerry收款1000元成功");
            }else {
                System.out.println("jerry收款1000元失败");
            }

        } catch (Exception throwables) {
            throwables.printStackTrace();
            //6. sql语句出现问题,回滚事务
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            //7. 关闭资源
            C3P0Util.release(con,null,null);
        }
    }
}
View Code

使用Utils包中的Utils类进行简化

 

posted @ 2020-08-27 14:50  硬盘红了  阅读(153)  评论(0编辑  收藏  举报