JAVA解决JDBC的自动提交带来的数据事务出错问题-----JDBC

package com.powernode.jdbc;

//DROP TABLE IF EXISTS t_act;
//CREATE TABLE t_act(actno BIGINT,balance DOUBLE(7,2));//7表示有效数字的个数,2表示小数位的个数
//INSERT INTO t_act(actno,balance) VALUES(111,20000);
//INSERT INTO t_act(actno,balance) VALUES(222,20000);
//COMMIT;
//SELECT * FROM t_act;
import java.sql.*;
import java.util.ResourceBundle;

public class JDBCTest04
{
    public static void main(String[] args)
    {
        //JDBC的事务都是自动提交的,什么叫自动提交(只要执行任意一条DML语句就会自动提交一次),这是JDBC默认的行为
        //在业务流程中我们通常是多条SQL语句(DML)同时执行,同时成功,同时失败
        //JDBC的事务是自动提交机制的,JDBC只要执行容易DML语句就提交一次
        PreparedStatement preparedStatement = null;
        Connection connection = null;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
        String url = resourceBundle.getString("url");
        String user = resourceBundle.getString("user");
        String password = resourceBundle.getString("password");
        try
        {
            Class.forName(resourceBundle.getString("Driver"));
            connection = DriverManager.getConnection(url,user,password);
            //将自动提交机制修改为手动提交,开启事务
            connection.setAutoCommit(false);
            String sql = "update t_act set balance = ? where actno = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"0.0");
            preparedStatement.setString(2,"222");
            int count = preparedStatement.executeUpdate();
            //终止程序的空指针异常
//            String s = null;
//            s.toString();

            preparedStatement.setString(1,"10000.0");
            preparedStatement.setString(2,"222");
            count = preparedStatement.executeUpdate() + count;
            System.out.println(count == 2 ? "转账成功" : "转账失败");
            //能走到这就是没有异常,手动提交数据
            connection.commit();
        }
        catch(Exception e)
        {
            if (connection != null)
            {
                try
                {
                    //回滚事务
                    connection.rollback();
                } catch (SQLException ex)
                {
                    throw new RuntimeException(ex);
                }
            }
            e.printStackTrace();
        }
        finally
        {
            if (preparedStatement != null)
            {
                try
                {
                    preparedStatement.close();
                }
                catch (SQLException e)
                {
                    throw new RuntimeException(e);
                }
            }
            if (connection != null)
            {
                try
                {
                    connection.close();
                }
                catch (SQLException e)
                {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

package com.powernode.jdbc;

//DROP TABLE IF EXISTS t_act;
//CREATE TABLE t_act(actno BIGINT,balance DOUBLE(7,2));//7表示有效数字的个数,2表示小数位的个数
//INSERT INTO t_act(actno,balance) VALUES(111,20000);
//INSERT INTO t_act(actno,balance) VALUES(222,20000);
//COMMIT;
//SELECT * FROM t_act;
import java.sql.*;
import java.util.ResourceBundle;

public class JDBCTest04
{
public static void main(String[] args)
{
//JDBC的事务都是自动提交的,什么叫自动提交(只要执行任意一条DML语句就会自动提交一次),这是JDBC默认的行为
//在业务流程中我们通常是多条SQL语句(DML)同时执行,同时成功,同时失败
//JDBC的事务是自动提交机制的,JDBC只要执行容易DML语句就提交一次
PreparedStatement preparedStatement = null;
Connection connection = null;
ResourceBundle resourceBundle = ResourceBundle.getBundle("JDBC");
String url = resourceBundle.getString("url");
String user = resourceBundle.getString("user");
String password = resourceBundle.getString("password");
try
{
Class.forName(resourceBundle.getString("Driver"));
connection = DriverManager.getConnection(url,user,password);
//将自动提交机制修改为手动提交,开启事务
connection.setAutoCommit(false);
String sql = "update t_act set balance = ? where actno = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,"0.0");
preparedStatement.setString(2,"222");
int count = preparedStatement.executeUpdate();
//终止程序的空指针异常
// String s = null;
// s.toString();

preparedStatement.setString(1,"10000.0");
preparedStatement.setString(2,"222");
count = preparedStatement.executeUpdate() + count;
System.out.println(count == 2 ? "转账成功" : "转账失败");
//能走到这就是没有异常,手动提交数据
connection.commit();
}
catch(Exception e)
{
if (connection != null)
{
try
{
//回滚事务
connection.rollback();
} catch (SQLException ex)
{
throw new RuntimeException(ex);
}
}
e.printStackTrace();
}
finally
{
if (preparedStatement != null)
{
try
{
preparedStatement.close();
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
}
if (connection != null)
{
try
{
connection.close();
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
}
}
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旧约Alatus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值