事务
当一个账户给另一个账户转账时,如果转账成功就提交事务,否则回滚事务
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class Trad {
public static void main(String[] args) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@//localhost:1521/ORCL";
String user = "scott";
String password = "123";
Connection conn = DriverManager.getConnection(url, user, password);
//关闭自动提交
conn.setAutoCommit(false);
try {
String sql1="update account set balance=balance-10000 where accid=10086";
String sql2="update account set balance=balance+10000 where accid=10010";
PreparedStatement ps= conn.prepareStatement(sql1);
ps.executeUpdate();
ps= conn.prepareStatement(sql2);
ps.executeUpdate();
conn.commit();
System.out.println("转账成功.....