jdbc实现事务相关

大致的实现思路(SQL语句不支持try-catch):

try {
	set autocommit =0; //在事务结束之前的所有DML处于同一个事务中。
	update account set money=money-100 where card_id='1234567890';
	update account set money=moy-100 where card_id='0515151515'
	commit; //提交事务,将DML语句执行结果持久化到磁盘。
}catch() {
	rollback;
    }

java代码完善:

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Text {

	public static void main(String[] args) {
		Connection connection = null;
		Statement statement=null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root","root");//2,建立连接
			connection.setAutoCommit(false);
			statement= connection.createStatement();
			statement.addBatch("update account set money=money-100 where card_id= '1234567890'");
			statement.addBatch("update account set money=money+100 where card_id= '0987654321'");
			statement.executeBatch();
			connection.commit();
		} catch (Exception e) {			
			try {
				if (connection!=null) {
					connection.rollback();
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally {
			try {
				if (statement!=null) {
					statement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (connection!=null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}	
	}
}

此时执行结果
在这里插入图片描述
思考:如果此时sql语句部分出错

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Text {

	public static void main(String[] args) {
		Connection connection = null;
		Statement statement=null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root","root");//2,建立连接
			connection.setAutoCommit(false);
			statement= connection.createStatement();
			statement.addBatch("update account set money=money-100 where card_id= '1234567890'");
			statement.addBatch("update account set mon=money+100 where card_id= '0987654321'");//出错的部分
			statement.executeBatch();
			connection.commit();
		} catch (Exception e) {			
			try {
				if (connection!=null) {
					connection.rollback();
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally {
			try {
				if (statement!=null) {
					statement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (connection!=null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}	
	}
}

控制台输出结果:
在这里插入图片描述
数据库输出结果
在这里插入图片描述
分析:由于其中一条SQL语句出错,则会被catch语句捕捉,输出对应异常信息,事务执行回滚,导致SQL语句对应的操作,上述输出结果则印证了这一点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值