JDBC事务操作(转账案例)和使用Utils改进
未使用事务
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/* 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); } }
在未使用事务时,默认使用mysql事务处理方式,此时如果出现异常,转账将会出现问题。
那么就需要我们使用事务,将两条sql语句必须要么都成功,要么都失败。
JDBC使用事务
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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); } } }
手动创建异常(此时回滚事务,两条sql语句都不会执行)
使用c3p0连接池时,需要的代码量很多,那么此时就可以使用Utils简化JDBC开发
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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); } } }
使用Utils包中的Utils类进行简化
![]()
学习的博客多用于在笔记中,防止笔记过于臃肿,所以将样例及运行结果放在博客中,后以超链接的形式记录在笔记中,所以有些博文过于单薄。如果有小伙伴遇到问题欢迎评论,看到就会回复,学渣一枚,加油努力。