package com.sw;
import java.sql.*;
public class TestTrans {
/**
* 事务举例
* 添加事务,提交事务,回滚
*/
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
//声明驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//加载连接
conn = DriverManager
.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl",
"scott","Oracle1");
//加入事物处理
conn.setAutoCommit(false);
//创建操作对象
stmt = conn.createStatement();
//从scott用户上减去100
stmt.executeUpdate("update emp set sal=sal-100 where ename='SCOTT'");
//给smith
stmt.executeUpdate("update emp set sal=sal+100 where ename='SMITH'");
//提交事务
conn.commit();
} catch (Exception e) {
// 如果发生异常则回滚
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}finally{
try {
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Oracle 事务的应用
最新推荐文章于 2024-04-11 20:56:54 发布