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();
                }
            }
        }
    }
}
                                    相关文章
暂无评论...
