当前位置:网站首页>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
边栏推荐
- ‘EddiesObservations‘ object has no attribute ‘filled‘
- 7-10 longest symmetric substring (25 points) (violence problem solution) C language
- MySQL lock mechanism
- 多线程与高并发(2)——synchronized用法详解
- 2-軟件設計原則
- Establish excel bookkeeping book through setting context menu
- SQL语句简单优化
- Frequently asked interview questions - 2 (computer network)
- mysql-触发器、存储过程、存储函数
- Issue 36 summary of atcoder beginer contest 248
猜你喜欢
基于ssm 包包商城系统
jdbc入门\获取数据库连接\使用PreparedStatement
Frequently asked interview questions - 2 (computer network)
转置卷积(Transposed Convolution)
手动删除eureka上已经注册的服务
Flutter nouvelle génération de rendu graphique Impeller
MySQL realizes master-slave replication / master-slave synchronization
Pilotage growth · ingenuity empowerment -- yonmaster developer training and pilotage plan is fully launched
Generation of straightening body in 3D slicer
Batch import of orange single micro service
随机推荐
js数字大写方法
第36期《AtCoder Beginner Contest 248 打比赛总结》
Object to map
The 8th Blue Bridge Cup 2017 - frog jumping cup
Pavlov and hobbies
2 - principes de conception de logiciels
Range of numbers (dichotomous classic template topic)
JVM系列(3)——内存分配与回收策略
Flutter 新一代图形渲染器 Impeller
Batch import of orange single micro service
TypeScript interface & type 粗略理解
Character recognition easyocr
Frequently asked interview questions - 2 (computer network)
Flutter 新一代圖形渲染器 Impeller
XXL job pit guide XXL RPC remoting error (connect timed out)
Common interview questions - 4 (MySQL)
多线程与高并发(1)——线程的基本知识(实现,常用方法,状态)
jdbc入门\获取数据库连接\使用PreparedStatement
Total score of [Huawei machine test] (how to deal with the wrong answer? Go back once to represent one wrong answer)
Navicate连接oracle(11g)时ORA:28547 Connection to server failed probable Oeacle Net admin error