当前位置:网站首页>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();
边栏推荐
- win10下使用msys+vs2019编译ffmpeg源码
- [go ~ 0 to 1] day 4 June 30 defer, structure, method
- Ffmpeg avframe to cv:: mat
- Why has instagram changed from a content sharing platform to a marketing tool? How do independent sellers use this tool?
- torch.nn.functional.interpolate函数
- Contos 7 set up SFTP to create users, user groups, and delete users
- Ffmpeg common commands (2)
- OpenCV视频质量检测--清晰度检测
- Dom4j parsing XML, XPath retrieving XML
- nacos启动失败问题解决与总结
猜你喜欢

pickle.load报错【AttributeError: Can‘t get attribute ‘Vocabulary‘ on <module ‘__main__‘】

Solution and summary of Nacos startup failure

The intelligent epidemic prevention system provides safety guarantee for the resumption of work and production at the construction site

白盒加密技术浅理解

微信公众号开发相关流程及功能介绍

GB28181之SIP协议

正则表达式=Regex=regular expression

见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约

Uni app product classification

音视频、编解码相关电子书、小工具,打包奉送!
随机推荐
Shell advanced
Use the uni app demo provided by Huanxin to quickly realize one-on-one chat
测试自学人必看:软件测试如何找测试项目?
XML syntax, constraints
采集抖音视频
Opencv video quality detection -- sharpness detection
H264编码profile & level控制
[Mori city] random talk on GIS data (I)
axure不显示元件库
Werewolf killing strategy: do you think I'm easy to cheat? Who do we believe!
Uni app wechat applet one click login to obtain permission function
Learning records of building thingsboard, an Internet of things platform
Dom4J解析XML、Xpath检索XML
下载(导出)pdf模板文件(比如:审批单),报错:Invalid nested tag *** found, expected closing tag ***
Basic use of MySQL
SIP protocol of gb28181
Extensive reading of the paper [film: visual reasoning with a general condition layer]
Flutter 实战-快速实现音视频通话应用
Live HLS protocol
Thesis reading [distinctive late semantic graph for video capturing]