当前位置:网站首页>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();
边栏推荐
猜你喜欢
Salesmartly has some tricks for Facebook chat!
Witness the times! "The future of Renji collaboration has come" 2022 Hongji ecological partnership conference opens live broadcast reservation
智慧防疫系统为建筑工地复工复产提供安全保障
What must be done in graduation season before going to Shanhai
Basic use of MySQL
XML语法、约束
AAAI2020: Real-time Scene Text Detection with Differentiable Binarization
Flutter 实战-快速实现音视频通话应用
[pytorch record] automatic hybrid accuracy training torch cuda. amp
wireshark报文分析tcp,ftp
随机推荐
Parallelism, concurrency and life cycle of threads
大厂音视频职位面试题目--今日头条
CMU AI PhD 第一年总结
Facebook聊单,SaleSmartly有妙招!
H264编码profile & level控制
sql查询去重统计的方法总结
DTD modeling
XML syntax, constraints
703. 数据流中的第 K 大元素
Instagram 为何从内容共享平台变成营销工具?独立站卖家如何利用该工具?
Botu V16 obtains the system time and converts it into a string
[pytorch record] distributed training dataparallel and distributeddataparallel of the model
Crunch简介、安装,使用Crunch制作密码字典
GB28181的NAT穿透
精耕渠道共谋发展 福昕携手伟仕佳杰开展新产品培训大会
Les canaux de culture intensive s'efforcent de développer Fu Xin et Wei Shi jiajie pour organiser une conférence de formation sur les nouveaux produits
Analysis of GetMessage underlying mechanism
MFC中如何重绘CListCtrl的表头
ffmpeg 错误码
Optaplanner learning notes (I) case cloud balance