当前位置:网站首页>JDBC中如何添加事务
JDBC中如何添加事务
2022-07-01 18:45:00 【欲与宇语】
问题描述
JDBC中如何添加事务
解决方案:
public static void main(String[] args) throws Exception {
//1.0注册驱动
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://01.01.01.01:3306/demo01";
String userName="demo01";
String passWord="123456";
Connection conn= DriverManager.getConnection(url,userName,passWord);
//定义sql
String sql ="update tb_account set money=money+500 where `Name`='张三'";
Statement stmt=conn.createStatement();
try{
//开启事务 false:手动提交 true:自动提交
conn.setAutoCommit(false);
//执行sql 获取影响行数
int count =stmt.executeUpdate(sql);
//把结果打印
System.out.println(count);
} catch(Exception throwables){
//回滚事务
conn.rollback();
throwables.printStackTrace();
}
//释放资源
stmt.close();;
conn.close();
边栏推荐
- Detailed explanation of JUnit unit test framework
- 商业智能BI开发和报表开发有什么本质区别?
- Reading the paper [learning to discretely compose reasoning module networks for video captioning]
- Why must we move from Devops to bizdevops?
- Solution and summary of Nacos startup failure
- IPv4 address, subnet mask, gateway
- Wireshark packet analysis TCP, FTP
- 音频编解码基础知识
- Basic knowledge of audio coding and decoding
- 【森城市】GIS数据漫谈(一)
猜你喜欢
[Mori city] random talk on GIS data (I)
Why has instagram changed from a content sharing platform to a marketing tool? How do independent sellers use this tool?
[go ~ 0 to 1] day 5 July 1 type alias, custom type, interface, package and initialization function
118. Yanghui triangle
EasyGBS主子码流都为H.265时,切换出现花屏如何解决?
Ubuntu14 install MySQL and configure root account local and remote access
Salesmartly has some tricks for Facebook chat!
如何正确使用Vertx操作Redis(3.9.4带源码分析)
pickle. Load error [attributeerror: can't get attribute 'volatile' on < module '\u main']
Dlib+opencv library for fatigue detection
随机推荐
After studying 11 kinds of real-time chat software, I found that they all have these functions
Parallelism, concurrency and life cycle of threads
Go Language Advanced
Task: denial of service DOS
如何正确使用Vertx操作Redis(3.9.4带源码分析)
703. 数据流中的第 K 大元素
GetMessage底层机制分析
线程的并行、并发、生命周期
Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference
PostgreSQL varchar[] array type operation
Implement a Prometheus exporter
Werewolf killing strategy: do you think I'm easy to cheat? Who do we believe!
[English grammar] Unit1 articles, nouns, pronouns and numerals
The use of subplot function in MATLAB
事务隔离级别 gap锁 死锁
241. Different Ways to Add Parentheses
微信公众号开发相关流程及功能介绍
直播HLS协议
OpenCV视频质量检测--清晰度检测
为什么一定要从DevOps走向BizDevOps?