ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

JDBC管理实务的概述和实现

2022-07-27 14:03:16  阅读:103  来源: 互联网

标签:事务 JDBC sql 概述 pstmt1 实务 pstmt2 null conn


JDBC管理实务的概述

事务:一个包含多个步骤的业务操作。如果这个业务操作被事务管理,则这对个步骤要么同时成功,要么同时失败。

操作:

  1、开启事务 

  2、提交事务

  3、回滚事务

使用Connection对象来管理事务

  开启事务:setAutoCommit(boolean autoCommit): 调用方法设置参数为false,即开启事务

    在执行sql之前开启事务

  提交事务:commit()

    在所有sql都执行完毕后提交事务

  回滚事务:rollback()

      在catch中回滚事务

  

 

 

JDBC管理实务的实现

 

/**
 * 事务操作
 */
public class JDBCDemo10 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt1 = null;
        PreparedStatement pstmt2 = null;
        try {
            //1.获取连接
            conn = JDBCUtils.getConnection();
            //2.定义sql
            //张三 -500
            String sql1 = "update account set balance = balance - ? where id = ?";
            //李四 +500
            String sql2 = "update account set balance = balance + ? where id = ?";
            //3.获取执行sql对象
            pstmt1 = conn.prepareStatement(sql1);
            pstmt2 = conn.prepareStatement(sql2);
            //4.设置参数
            pstmt1.setDouble(1,500);
            pstmt1.setInt(2,1);
            pstmt2.setDouble(1,500);
            pstmt2.setInt(2,2);
            //5.执行sql
            pstmt1.executeUpdate();
            int i  = 3/0;
            pstmt2.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(pstmt1,conn);
            JDBCUtils.close(pstmt2,null);
        }
    }
}

当代码执行完第一条sql之后程序报错,第二条sql没有执行,张三的金额已经减了,但是李四的金额没有增加。

所以我们要使用事务来解决

 

 

解决方式:

/**
 * 事务操作
 */
public class JDBCDemo10 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement pstmt1 = null;
        PreparedStatement pstmt2 = null;
        try {
            //1.获取连接
            conn = JDBCUtils.getConnection();

            //开启事务
            conn.setAutoCommit(false);

            //2.定义sql
            //张三 -500
            String sql1 = "update account set balance = balance - ? where id = ?";
            //李四 +500
            String sql2 = "update account set balance = balance + ? where id = ?";
            //3.获取执行sql对象
            pstmt1 = conn.prepareStatement(sql1);
            pstmt2 = conn.prepareStatement(sql2);
            //4.设置参数
            pstmt1.setDouble(1,500);
            pstmt1.setInt(2,1);
            pstmt2.setDouble(1,500);
            pstmt2.setInt(2,2);
            //5.执行sql
            pstmt1.executeUpdate();
            int i  = 3/0;
            pstmt2.executeUpdate();

            //提交事务
            conn.commit();
        } catch (Exception throwables) {
            //事务的回滚
            try {
                if (conn!=null){
                    conn.rollback();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(pstmt1,conn);
            JDBCUtils.close(pstmt2,null);
        }
    }
}

执行之后代码找样会报错

 

 但是数据库中的内容没有发生变化

 

标签:事务,JDBC,sql,概述,pstmt1,实务,pstmt2,null,conn
来源: https://www.cnblogs.com/xjw12345/p/16524522.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有