当前位置:网站首页>JDBC操作事务
JDBC操作事务
2022-04-23 05:41:00 【hanyc..】
package com.hyc.study04;
import com.hyc.study02.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestTransaction {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JDBCUtils.getConnection();
//关闭事务自动提交,自动开启事务
connection.setAutoCommit(false);
String sqlOut = "UPDATE `account` SET `money`=money-100 WHERE `id`=?";
preparedStatement = connection.prepareStatement(sqlOut);
preparedStatement.setInt(1,1);
preparedStatement.executeUpdate();
String sqlIn = "UPDATE `account` SET `money`=money+100 WHERE `id`=?";
preparedStatement = connection.prepareStatement(sqlIn);
preparedStatement.setInt(1,2);
preparedStatement.executeUpdate();
//业务操作完毕,提交事务
connection.commit();
} catch (SQLException throwables) {
try {
connection.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
throwables.printStackTrace();
}finally {
JDBCUtils.release(connection,preparedStatement,resultSet);
}
}
}
版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42732184/article/details/124276560
边栏推荐
猜你喜欢
多线程与高并发(1)——线程的基本知识(实现,常用方法,状态)
Some pits used by uni
2 - software design principles
Deep learning object detection
MySQL realizes master-slave replication / master-slave synchronization
Sea Level Anomaly 和 Sea Surface Height Anomaly 的区别
C语言——恶搞关机小程序
Flutter 新一代图形渲染器 Impeller
JVM系列(3)——内存分配与回收策略
Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)
随机推荐
Strategies to improve Facebook's touch rate and interaction rate | intelligent customer service helps you grasp users' hearts
框架解析1.系统架构简介
‘EddiesObservations‘ object has no attribute ‘filled‘
JS number capitalization method
Character recognition easyocr
Batch import of orange single micro service
Golang implements Ping connectivity detection case through exec module
JVM系列(3)——内存分配与回收策略
Split and merge multiple one-dimensional arrays into two-dimensional arrays
Pavlov and hobbies
The QT debug version runs normally and the release version runs crash
Shell instruction learning 1
Map对象 map.get(key)
Transposed convolution
引航成长·匠心赋能——YonMaster开发者培训领航计划全面开启
Common protocols of OSI layer
Frequently asked interview questions - 1 (non technical)
windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
Excel obtains the difference data of two columns of data
qt. qpa. plugin: Could not find the Qt platform plugin “xcb“ in ““