A sample program that uses JDBC to process transaction
Import java. SQL .*;
Public class TestJDBC {
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 :@ localhost: 1521: ORCL", "scott", "tiger ");
Conn. setAutoCommit (false );
Stmt = conn. createStatement ();
Stmt. addBatch ("insert into dept2 values (61, 'test', 'apple ')");
Stmt. addBatch ("insert into dept2 values (62, 'test', 'apple ')");
Stmt. addBatch ("insert into dept2 values (63, 'test', 'apple ')");
Stmt.exe cuteBatch ();
Conn. commit ();
Conn. setAutoCommit (true );
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
} Catch (SQLException e ){
If (conn! = Null ){
Try {
Conn. rollback ();
Conn. setAutoCommit (true );
} Catch (SQLException e1 ){
E1.printStackTrace ();
}
}
E. printStackTrace ();
}
Finally {
If (stmt! = Null ){
Try {
Stmt. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
If (conn! = Null ){
Try {
Conn. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
}
}
}