您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
03-JDBC-API详解-Connection(事务处理)
发布时间:2024-11-13 13:49:31编辑:雪饮阅读()
-
IntelliJ IDEA中可以用快捷键ctrl+alt+t对选中区块的代码进行语句包裹,会出现一个选择包裹语句的列表,然后我们可以从这个列表中选择语句,例如常见的try/catch语句。
那么接下来实现事务处理如
try {
conn.setAutoCommit(false);
String sql="update account set money=2000 where id = 1";
int count=stmt.executeUpdate(sql);
System.out.println("操作1=》影响行数:"+count);
String sql2="update account set money=2000 where id = 2";
int count2=stmt.executeUpdate(sql2);
System.out.println("操作2=》影响行数:"+count2);
conn.commit();
} catch (Exception throwables) {
conn.rollback();
throwables.printStackTrace();
}
关键字词:jdbc,事务处理