jdbc 10:jdbc事务

2年前 (2022) 程序员胖胖胖虎阿
291 0 0

jdbc连接mysql,涉及到的事务问题

package com.examples.jdbc.o10_jdbc事务;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ResourceBundle;

/**
 * jdbc事务演示:单机转帐
 */
public class Test {
    public static void main(String[] args) {
        //countUnsafe();
        countSafe();
    }

    /**
     * 单机转帐:开启事务
     */
    private static void countSafe() {

        //资源绑定器绑定配置属性文件
        ResourceBundle resourceBundle = ResourceBundle.getBundle("config/jdbc");
        String driver = resourceBundle.getString("driver");
        String url = resourceBundle.getString("url");
        String userName = resourceBundle.getString("userName");
        String passWord = resourceBundle.getString("passWord");

        //2个资源文件
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        //jdbc6步骤

        try {
            //1.
            Class.forName(driver);

            //2.
            connection = DriverManager.getConnection(url, userName, passWord);

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

            //3.
            String sql = "update tb_count set money = ? where count = ?";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setDouble(1, 10000);
            preparedStatement.setInt(2, 111);

            int num = preparedStatement.executeUpdate();

            //String s = null;
            //s.toString();

            preparedStatement.setDouble(1, 10000);
            preparedStatement.setInt(2, 222);
            num += preparedStatement.executeUpdate();

            System.out.println(num == 2 ? "转帐成功" : "转帐失败");

            connection.commit();    //提交事务

        } catch (ClassNotFoundException | SQLException e) {

            //事务回滚
            if(connection != null){
                try {
                    connection.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            e.printStackTrace();
        }finally {
            //6.
            if(preparedStatement != null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 单机转帐:未开启事务
     */
    private static void countUnsafe() {

        //资源绑定器绑定配置属性文件
        ResourceBundle resourceBundle = ResourceBundle.getBundle("config/jdbc");
        String driver = resourceBundle.getString("driver");
        String url = resourceBundle.getString("url");
        String userName = resourceBundle.getString("userName");
        String passWord = resourceBundle.getString("passWord");

        //2个资源文件
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        //jdbc6步骤

        try {
            //1.
            Class.forName(driver);

            //2.
            connection = DriverManager.getConnection(url, userName, passWord);

            //3.
            String sql = "update tb_count set money = ? where count = ?";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setDouble(1, 10000);
            preparedStatement.setInt(2, 111);

            int num = preparedStatement.executeUpdate();

            String s = null;
            s.toString();

            preparedStatement.setDouble(1, 10000);
            preparedStatement.setInt(2, 222);
            num += preparedStatement.executeUpdate();

            System.out.println(num == 2 ? "转帐成功" : "转帐失败");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            //6.
            if(preparedStatement != null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
版权声明:程序员胖胖胖虎阿 发表于 2022年9月18日 下午3:48。
转载请注明:jdbc 10:jdbc事务 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...