import java.sql.*;
class jdbcTransactions2 {
  public static void main (String args [])
   throws SQLException {
 // Oracle-Treiber laden
 DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
 // Verbindung zur Datenbank herstellen
 String url = "jdbc:oracle:thin:@oracle11.informatik.uni-goettingen.de:1521:dbis";
 Connection conn1 = DriverManager.getConnection(url,"scott","tiger");
 conn1.setAutoCommit(false);
 Statement stmt1 = conn1.createStatement();
 stmt1.execute("DROP TABLE TTEST");
 stmt1.execute("CREATE TABLE TTEST(A VARCHAR2(4))");
 stmt1.execute("COMMIT");
 PreparedStatement pstmt1 = conn1.prepareStatement("INSERT INTO TTEST VALUES(?)");
 pstmt1.setString(1, "1BLA");

 // Variante I: zum batch dazu und erst ganz spaeter ausfuehren.
 // pstmt1.addBatch();
 // // pstmt1.executeBatch();
 
 // Variante II: execute jedesmal:
 pstmt1.execute();
 //conn1.commit(); // wenn man das weglaesst mault stmt2.exec (INSERT 2FOO) wegen java.sql.SQLException: ORA-00054: Ressource belegt und Anforderung mit NOWAIT angegeben oder Timeout abgelaufen

 System.out.println ("select from conn1:"); 
 ResultSet rset1 = stmt1.executeQuery("SELECT A FROM TTEST");
 while (rset1.next()) {
  String s = rset1.getString(1);
  System.out.println (s); }

 // second conn:
 // Connection conn2 = conn1;  use the same -> commit is not required
 Connection conn2 = DriverManager.getConnection(url,"scott","tiger");
 // Anfrage an die Datenbank
 Statement stmt2 = conn2.createStatement();
 stmt2.execute("INSERT INTO TTEST VALUES('2FOO')");
 stmt2.execute("ALTER TABLE TTEST MODIFY (A VARCHAR2(7))");

 System.out.println ("select from conn2:"); 
 ResultSet rset2 = stmt2.executeQuery("SELECT A FROM TTEST");
 while (rset2.next()) {
  String s = rset2.getString(1);
  System.out.println (s); }

 // zu Variante I: nochwas in den Batch und exec:
 pstmt1.setString(1, "1BLABLA");
 pstmt1.addBatch();
 pstmt1.executeBatch();

 System.out.println ("select from conn1:"); 
 ResultSet rset3 = stmt1.executeQuery("SELECT A FROM TTEST");
 while (rset3.next()) {
  String s = rset3.getString(1);
  System.out.println (s); }

 stmt1.execute("COMMIT");
 conn1.close();
 conn2.close();

}}
