import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class OracleTransaction {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con = null;
PreparedStatement ps = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger");
con.setAutoCommit(false);
ps = con.prepareStatement("update myemp set sal = sal + ? where ename = ?");
ps.setDouble(1, 1000);
ps.setString(2, "WARD");
ps.executeUpdate();
//int i = 10/0;
ps = con.prepareStatement("update myemp set sal = sal - ? where ename = ?");
ps.setDouble(1, 1000);
ps.setString(2, "ALLEN");
ps.executeUpdate();
con.commit();
} catch (Exception e) {
try {
con.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
} finally {
try {
ps.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}