/* Simple example that just demonstrates transactions.
   conn2 sees only what it has written itself. Things written by
   conn1, but not yet committed are invisible */

import java.sql.*;
class jdbcTransactions {
  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");
 stmt1.execute("INSERT INTO TTEST VALUES('1BLA')");
 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 = DriverManager.getConnection(url,"scott","tiger");
 // Anfrage an die Datenbank
 Statement stmt2 = conn2.createStatement();
 stmt2.execute("INSERT INTO TTEST VALUES('2FOO')"); 
 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); }

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

}}
