栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JDBC(五)—事务(动力节点杜老师)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JDBC(五)—事务(动力节点杜老师)

背景

jdbc单个库的事务实际上是很简单的,原本觉得没必要写,但是想了想还是写了。

简介

jdbc的事务默认是自动开启的,也就是,每执行一次sql就直接会提交事务,这样的话就会有个问题,当我们要执行多条SQL语句时,如果只有前面几条语句执行成功,但是后面的SQL语却执行失败的话,那么数据库中就会残留执行成功的数据,而执行失败的语句则自然不会有数据残留,但是这却会导致数据不匹配、不完整,这样也就会出错

解决方案

最后统一提交事务,如果有失败的就不提交,直接回滚。

connection.setAutoCommit(false);//关闭自动提交connection.commit();//手动提交connection.rollback();//异常,回滚

import java.sql.*;

public class JDBC11 {
    public static void main(String[] args){

        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try{
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/yhsql","root","root");
//            关闭自动提交,改为手动提交
            connection.setAutoCommit(false);

            String sql1 = "insert into dept (deptno,dname,loc) values (?,?,?)";
            preparedStatement = connection.prepareStatement(sql1);
            preparedStatement.setInt(1,99);
            preparedStatement.setString(2,"狗日");
            preparedStatement.setString(3,"阿萨德");
            int i = preparedStatement.executeUpdate();
            System.out.println(i == 1 ? "添加数据成功":"添加数据失败");
            try {
                Thread.sleep(8000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            String sql2 = "delete from dept where deptno = ?";
            preparedStatement = connection.prepareStatement(sql2);
            preparedStatement.setInt(1,60);
             i += preparedStatement.executeUpdate();
            System.out.println(i == 2 ? "删除数据成功":"删除数据失败");

            try {
                Thread.sleep(8000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            String sql3 = "update dept set deptno = 60 where deptno = ?";
            preparedStatement = connection.prepareStatement(sql3);
            preparedStatement.setInt(1,99);
             i += preparedStatement.executeUpdate();
            System.out.println(i == 3 ? "更新数据成功":"更新数据失败");

            System.out.printf("事务全部执行完毕,开始提交...已提交");
            connection.commit();


        } catch (Exception e) {
            e.printStackTrace();
            if ( connection != null){
                try{
                    connection.rollback();
                } catch (SQLException e2) {
                    e2.printStackTrace();
                }
            }
        }
        finally{
            try{
                if ( connection!=null)
                    connection.close();
            }
            catch(SQLException e){
                e.printStackTrace();
            }
            try {
                if ( preparedStatement!=null)
                    preparedStatement.close();
            }catch(SQLException e){
                e.printStackTrace();
            }

        }

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/760801.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号