Java by API/java.sql/Connection — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:46, 31 мая 2010

Connection: close()

  
/* 
 * */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    if (conn != null) {
      System.out.println("Connection attempt was successful!");
      try {
        conn.close();
      } catch (SQLException ex) {
        ex.printStackTrace();
        System.out.println("Problem closing the connection");
      }
    }
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





Connection: commit()

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,myURL CHAR);");
    String str = conn.nativeSQL("insert into survey(id) values(02)");
    System.out.println(str);
    
    conn.rumit();    
    st.close();
    conn.close();
  }
  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
}





Connection: createStatement()

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    setup();
  }
  static Connection conn;
  static Statement st;
  public static void setup() {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





Connection: createStatement(int resultSetType, int resultSetConcurrency)

  

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);
    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);
    // Create a statement that will return updatable result sets
    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    // Primary key must be specified so that the result set is updatable
    ResultSet resultSet = stmt.executeQuery("SELECT col_string FROM my_table");
  }
}





Connection: getAutoCommit()

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    boolean executeResult;
    try {
      String driver = "oracle.jdbc.driver.OracleDriver";
      Class.forName(driver).newInstance();
      System.out.println("Connecting to database...");
      String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
      conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
      stmt = conn.createStatement();
      conn.setAutoCommit(false);
      if (!conn.getAutoCommit())
        System.out.println("Auto-commit is set to false");
      String sql = "INSERT INTO Location VALUES(715,"Houston")";
      stmt.executeUpdate(sql);
      sql = "INSERT INTO Employees VALUES" + "(8,"K","4351",{d "2000-02-00"},715)";
      stmt.executeUpdate(sql);
      conn.rumit();
    } catch (SQLException se) {
      String msg = se.getMessage();
      msg = "SQLException occured with message: " + msg;
      System.out.println(msg);
      System.out.println("Starting rollback operations...");
      try {
        conn.rollback();
      } catch (SQLException se2) {
        se2.printStackTrace();
      }
    } catch (Exception e) {
      e.printStackTrace();
    } 
  }
}





Connection: getMetaData()

  
/*
Driver Loaded.
Got Connection.
SYSTEM_ALIASES
SYSTEM_ALLTYPEINFO
SYSTEM_AUTHORIZATIONS
SYSTEM_BESTROWIDENTIFIER
SYSTEM_CACHEINFO
SYSTEM_CATALOGS
SYSTEM_CHECK_COLUMN_USAGE
SYSTEM_CHECK_CONSTRAINTS
SYSTEM_CHECK_ROUTINE_USAGE
SYSTEM_CHECK_TABLE_USAGE
SYSTEM_CLASSPRIVILEGES
SYSTEM_COLLATIONS
SYSTEM_COLUMNPRIVILEGES
SYSTEM_COLUMNS
SYSTEM_CROSSREFERENCE
SYSTEM_INDEXINFO
SYSTEM_PRIMARYKEYS
SYSTEM_PROCEDURECOLUMNS
SYSTEM_PROCEDURES
SYSTEM_PROPERTIES
SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS
SYSTEM_SCHEMAS
SYSTEM_SCHEMATA
SYSTEM_SEQUENCES
SYSTEM_SESSIONINFO
SYSTEM_SESSIONS
SYSTEM_SUPERTABLES
SYSTEM_SUPERTYPES
SYSTEM_TABLEPRIVILEGES
SYSTEM_TABLES
SYSTEM_TABLETYPES
SYSTEM_TABLE_CONSTRAINTS
SYSTEM_TEXTTABLES
SYSTEM_TRIGGERCOLUMNS
SYSTEM_TRIGGERS
SYSTEM_TYPEINFO
SYSTEM_UDTATTRIBUTES
SYSTEM_UDTS
SYSTEM_USAGE_PRIVILEGES
SYSTEM_USERS
SYSTEM_VERSIONCOLUMNS
SYSTEM_VIEWS
SYSTEM_VIEW_COLUMN_USAGE
SYSTEM_VIEW_ROUTINE_USAGE
SYSTEM_VIEW_TABLE_USAGE 
 * */
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    DatabaseMetaData md = conn.getMetaData();
    ResultSet rs = md.getTables(null, null, "%", null);
    while (rs.next()) {
      System.out.println(rs.getString(3));
    }  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





Connection: getTransactionIsolation()

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    int tx = conn.getMetaData().getDefaultTransactionIsolation();
    String txtxt=null;
    switch(tx) {
      case Connection.TRANSACTION_NONE :
        txtxt = "TRANSACTION_NONE"; break;
      case Connection.TRANSACTION_READ_COMMITTED :
        txtxt = "TRANSACTION_READ_COMMITTED"; break;
      case Connection.TRANSACTION_READ_UNCOMMITTED :
        txtxt = "TRANSACTION_READ_UNCOMMITTED"; break;
      case Connection.TRANSACTION_REPEATABLE_READ :
        txtxt = "TRANSACTION_REPEATABLE_READ"; break;
          case Connection.TRANSACTION_SERIALIZABLE :
        txtxt = "TRANSACTION_SERIALIZABLE"; break;
      default:
        txtxt = "UNKNOWN!!";
    }
    System.out.println(txtxt);
    conn.setTransactionIsolation(tx);
    System.out.println("Done");
    conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    System.out.println("TX is now " + conn.getTransactionIsolation());
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





Connection: getWarnings()

  
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLWarning;
public class Main {
  public static void main(String[] args) throws Exception {
    String dbURL = "jdbc:odbc:Companies";
    try {
      // Load the jdbc-odbc bridge driver
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      // Enable logging
      DriverManager.setLogWriter(new PrintWriter((System.err)));
      System.out.println("Getting Connection");
      Connection conn = DriverManager.getConnection(dbURL, "user", "password");
      SQLWarning warn = conn.getWarnings();
      while (warn != null) {
        System.out.println("SQLState: " + warn.getSQLState());
        System.out.println("Message:  " + warn.getMessage());
        System.out.println("Vendor:   " + warn.getErrorCode());
        System.out.println("");
        warn = warn.getNextWarning();
      }
      conn.close();
    } catch (ClassNotFoundException e) {
      System.out.println("Can"t load driver " + e);
    } catch (SQLException e) {
      System.out.println("Database access failed " + e);
    }
  }
}





Connection: nativeSQL(String sql)

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,myURL CHAR);");
    String str = conn.nativeSQL("insert into survey(id) values(02)");
    System.out.println(str);
    
    conn.rumit();    
    st.close();
    conn.close();
  }
  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
}





Connection: prepareCall(String sql)

  
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    CallableStatement cs = conn.prepareCall("{call StoreProcedureName}");
    ResultSet rs = cs.executeQuery();
    while (rs.next()) {
      System.out.println("Defunct user: " + rs.getString("user"));
    }
  }
  static Connection conn;
  static Statement st;
  public static void setup() {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





Connection: prepareStatement(String sql)

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Main {
  public static void main(String[] args) throws Exception {
    try {
      String url = "jdbc:odbc:databaseName";
      String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String user = "guest";
      String password = "guest";
      Class.forName(driver);
      Connection connection = DriverManager.getConnection(url,user, password);
      String changeLastName = "UPDATE authors SET lastname = ? WHERE authid = ?";
      PreparedStatement updateLastName = connection.prepareStatement(changeLastName);
      updateLastName.setString(1, "Martin"); // Set lastname placeholder value
      updateLastName.setInt(2, 4); // Set author ID placeholder value
      int rowsUpdated = updateLastName.executeUpdate(); // execute the update
      System.out.println("Rows affected: " + rowsUpdated);
      connection.close();
    } catch (ClassNotFoundException cnfe) {
      System.err.println(cnfe);
    } catch (SQLException sqle) {
      System.err.println(sqle);
    }
  }
}





Connection: rollback()

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    
    conn.setAutoCommit(false);
    Statement st = conn.createStatement();
    try {
      st.executeUpdate("create table survey (id int,name varchar(30));");
      st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
      st.executeUpdate("insert into survey (id,name ) values (2,"nameValue")");
       // commits all the transactions
       conn.rumit();
    }
    catch (Exception e){
       //cancel (roll back) all the transactions
       conn.rollback();
       // to see what went wrong
       e.printStackTrace();
    }
    
    
    
    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");
    outputResultSet(rs);
    rs.close();
    st.close();
    conn.close();
  }
  private static void outputResultSet(ResultSet rs) throws Exception {
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();
    for (int i = 1; i < numberOfColumns + 1; i++) {
      String columnName = rsMetaData.getColumnName(i);
      System.out.print(columnName + "   ");
    }
    System.out.println();
    System.out.println("----------------------");
    while (rs.next()) {
      for (int i = 1; i < numberOfColumns + 1; i++) {
        System.out.print(rs.getString(i) + "   ");
      }
      System.out.println();
    }
  }
  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    return DriverManager.getConnection(url, "sa", "");
  }
}





Connection: rollback(Savepoint savepoint)

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
public class MainClass {
  public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;
    try {
      Class.forName("org.hsqldb.jdbcDriver").newInstance();
      String url = "jdbc:hsqldb:hsqldb\\demoDatabase";
      connection = DriverManager.getConnection(url, "username", "password");
      connection.setAutoCommit(false);
      statement = connection.createStatement();
      String update1 = "UPDATE employees SET email = "a@b.ru" WHERE email = "a@a.ru"";
      statement.executeUpdate(update1);
      Savepoint savepoint1 = connection.setSavepoint("savepoint1");
      String update2 = "UPDATE employees SET email = "b@b.ru" WHERE email = "b@c.ru"";
      statement.executeUpdate(update2);
      Savepoint savepoint2 = connection.setSavepoint("savepoint2");
      String update3 = "UPDATE employees SET email = "c@c.ru" WHERE email = "c@d.ru"";
      statement.executeUpdate(update3);
      Savepoint savepoint3 = connection.setSavepoint("savepoint3");
      String update4 = "UPDATE employees SET email = "d@d.ru" WHERE email = "d@e.ru"";
      statement.executeUpdate(update4);
      Savepoint savepoint4 = connection.setSavepoint("savepoint4");
      String update5 = "UPDATE employees SET email = "e@e.ru" WHERE email = "e@f.ru"";
      statement.executeUpdate(update5);
      Savepoint savepoint5 = connection.setSavepoint("savepoint5");
      connection.rollback(savepoint3);
      connection.rumit();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (statement != null) {
        try {
          statement.close();
        } catch (SQLException e) {
        } // nothing we can do
      }
      if (connection != null) {
        try {
          connection.close();
        } catch (SQLException e) {
        } // nothing we can do
      }
    }
  }
}





Connection: setAutoCommit(boolean autoCommit)

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();
    conn.setAutoCommit(false);
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,"nameValue")");
    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");
    outputResultSet(rs);
    rs.close();
    st.close();
    conn.close();
  }
  private static void outputResultSet(ResultSet rs) throws Exception {
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int numberOfColumns = rsMetaData.getColumnCount();
    for (int i = 1; i < numberOfColumns + 1; i++) {
      String columnName = rsMetaData.getColumnName(i);
      System.out.print(columnName + "   ");
    }
    while (rs.next()) {
      for (int i = 1; i < numberOfColumns + 1; i++) {
        System.out.print(rs.getString(i) + "   ");
      }
      System.out.println();
    }
  }
  private static Connection getHSQLConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";
    DriverManager.setLoginTimeout(60); // fail after 60 seconds
    return DriverManager.getConnection(url, "sa", "");
  }
}





Connection: setSavepoint(String name)

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
public class MainClass {
  public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;
    try {
      Class.forName("org.hsqldb.jdbcDriver").newInstance();
      String url = "jdbc:hsqldb:hsqldb\\demoDatabase";
      connection = DriverManager.getConnection(url, "username", "password");
      connection.setAutoCommit(false);
      statement = connection.createStatement();
      String update1 = "UPDATE employees SET email = "a@b.ru" WHERE email = "a@a.ru"";
      statement.executeUpdate(update1);
      Savepoint savepoint1 = connection.setSavepoint("savepoint1");
      String update2 = "UPDATE employees SET email = "b@b.ru" WHERE email = "b@c.ru"";
      statement.executeUpdate(update2);
      Savepoint savepoint2 = connection.setSavepoint("savepoint2");
      String update3 = "UPDATE employees SET email = "c@c.ru" WHERE email = "c@d.ru"";
      statement.executeUpdate(update3);
      Savepoint savepoint3 = connection.setSavepoint("savepoint3");
      String update4 = "UPDATE employees SET email = "d@d.ru" WHERE email = "d@e.ru"";
      statement.executeUpdate(update4);
      Savepoint savepoint4 = connection.setSavepoint("savepoint4");
      String update5 = "UPDATE employees SET email = "e@e.ru" WHERE email = "e@f.ru"";
      statement.executeUpdate(update5);
      Savepoint savepoint5 = connection.setSavepoint("savepoint5");
      connection.rollback(savepoint3);
      connection.rumit();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (statement != null) {
        try {
          statement.close();
        } catch (SQLException e) {
        } // nothing we can do
      }
      if (connection != null) {
        try {
          connection.close();
        } catch (SQLException e) {
        } // nothing we can do
      }
    }
  }
}





Connection: setTransactionIsolation(int level)

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    int tx = conn.getMetaData().getDefaultTransactionIsolation();
    String txtxt=null;
    switch(tx) {
      case Connection.TRANSACTION_NONE :
        txtxt = "TRANSACTION_NONE"; break;
      case Connection.TRANSACTION_READ_COMMITTED :
        txtxt = "TRANSACTION_READ_COMMITTED"; break;
      case Connection.TRANSACTION_READ_UNCOMMITTED :
        txtxt = "TRANSACTION_READ_UNCOMMITTED"; break;
      case Connection.TRANSACTION_REPEATABLE_READ :
        txtxt = "TRANSACTION_REPEATABLE_READ"; break;
          case Connection.TRANSACTION_SERIALIZABLE :
        txtxt = "TRANSACTION_SERIALIZABLE"; break;
      default:
        txtxt = "UNKNOWN!!";
    }
    System.out.println(txtxt);
    conn.setTransactionIsolation(tx);
    System.out.println("Done");
    conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    System.out.println("TX is now " + conn.getTransactionIsolation());
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





Connection.TRANSACTION_NONE

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    int tx = conn.getMetaData().getDefaultTransactionIsolation();
    String txtxt=null;
    switch(tx) {
      case Connection.TRANSACTION_NONE :
        txtxt = "TRANSACTION_NONE"; break;
      case Connection.TRANSACTION_READ_COMMITTED :
        txtxt = "TRANSACTION_READ_COMMITTED"; break;
      case Connection.TRANSACTION_READ_UNCOMMITTED :
        txtxt = "TRANSACTION_READ_UNCOMMITTED"; break;
      case Connection.TRANSACTION_REPEATABLE_READ :
        txtxt = "TRANSACTION_REPEATABLE_READ"; break;
          case Connection.TRANSACTION_SERIALIZABLE :
        txtxt = "TRANSACTION_SERIALIZABLE"; break;
      default:
        txtxt = "UNKNOWN!!";
    }
    System.out.println(txtxt);
    conn.setTransactionIsolation(tx);
    System.out.println("Done");
    conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    System.out.println("TX is now " + conn.getTransactionIsolation());
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





Connection.TRANSACTION_READ_COMMITTED

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    int tx = conn.getMetaData().getDefaultTransactionIsolation();
    String txtxt=null;
    switch(tx) {
      case Connection.TRANSACTION_NONE :
        txtxt = "TRANSACTION_NONE"; break;
      case Connection.TRANSACTION_READ_COMMITTED :
        txtxt = "TRANSACTION_READ_COMMITTED"; break;
      case Connection.TRANSACTION_READ_UNCOMMITTED :
        txtxt = "TRANSACTION_READ_UNCOMMITTED"; break;
      case Connection.TRANSACTION_REPEATABLE_READ :
        txtxt = "TRANSACTION_REPEATABLE_READ"; break;
          case Connection.TRANSACTION_SERIALIZABLE :
        txtxt = "TRANSACTION_SERIALIZABLE"; break;
      default:
        txtxt = "UNKNOWN!!";
    }
    System.out.println(txtxt);
    conn.setTransactionIsolation(tx);
    System.out.println("Done");
    conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    System.out.println("TX is now " + conn.getTransactionIsolation());
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





Connection.TRANSACTION_READ_UNCOMMITTED

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    int tx = conn.getMetaData().getDefaultTransactionIsolation();
    String txtxt=null;
    switch(tx) {
      case Connection.TRANSACTION_NONE :
        txtxt = "TRANSACTION_NONE"; break;
      case Connection.TRANSACTION_READ_COMMITTED :
        txtxt = "TRANSACTION_READ_COMMITTED"; break;
      case Connection.TRANSACTION_READ_UNCOMMITTED :
        txtxt = "TRANSACTION_READ_UNCOMMITTED"; break;
      case Connection.TRANSACTION_REPEATABLE_READ :
        txtxt = "TRANSACTION_REPEATABLE_READ"; break;
          case Connection.TRANSACTION_SERIALIZABLE :
        txtxt = "TRANSACTION_SERIALIZABLE"; break;
      default:
        txtxt = "UNKNOWN!!";
    }
    System.out.println(txtxt);
    conn.setTransactionIsolation(tx);
    System.out.println("Done");
    conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    System.out.println("TX is now " + conn.getTransactionIsolation());
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





Connection.TRANSACTION_REPEATABLE_READ

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    int tx = conn.getMetaData().getDefaultTransactionIsolation();
    String txtxt=null;
    switch(tx) {
      case Connection.TRANSACTION_NONE :
        txtxt = "TRANSACTION_NONE"; break;
      case Connection.TRANSACTION_READ_COMMITTED :
        txtxt = "TRANSACTION_READ_COMMITTED"; break;
      case Connection.TRANSACTION_READ_UNCOMMITTED :
        txtxt = "TRANSACTION_READ_UNCOMMITTED"; break;
      case Connection.TRANSACTION_REPEATABLE_READ :
        txtxt = "TRANSACTION_REPEATABLE_READ"; break;
          case Connection.TRANSACTION_SERIALIZABLE :
        txtxt = "TRANSACTION_SERIALIZABLE"; break;
      default:
        txtxt = "UNKNOWN!!";
    }
    System.out.println(txtxt);
    conn.setTransactionIsolation(tx);
    System.out.println("Done");
    conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    System.out.println("TX is now " + conn.getTransactionIsolation());
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}





Connection.TRANSACTION_SERIALIZABLE

  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    int tx = conn.getMetaData().getDefaultTransactionIsolation();
    String txtxt=null;
    switch(tx) {
      case Connection.TRANSACTION_NONE :
        txtxt = "TRANSACTION_NONE"; break;
      case Connection.TRANSACTION_READ_COMMITTED :
        txtxt = "TRANSACTION_READ_COMMITTED"; break;
      case Connection.TRANSACTION_READ_UNCOMMITTED :
        txtxt = "TRANSACTION_READ_UNCOMMITTED"; break;
      case Connection.TRANSACTION_REPEATABLE_READ :
        txtxt = "TRANSACTION_REPEATABLE_READ"; break;
          case Connection.TRANSACTION_SERIALIZABLE :
        txtxt = "TRANSACTION_SERIALIZABLE"; break;
      default:
        txtxt = "UNKNOWN!!";
    }
    System.out.println(txtxt);
    conn.setTransactionIsolation(tx);
    System.out.println("Done");
    conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    System.out.println("TX is now " + conn.getTransactionIsolation());
  }
  static Connection conn;
  static Statement st;
  static {
    try {
      // Step 1: Load the JDBC driver.
      Class.forName("org.hsqldb.jdbcDriver");
      System.out.println("Driver Loaded.");
      // Step 2: Establish the connection to the database.
      String url = "jdbc:hsqldb:data/tutorial";
      conn = DriverManager.getConnection(url, "sa", "");
      System.out.println("Got Connection.");
      st = conn.createStatement();
    } catch (Exception e) {
      System.err.println("Got an exception! ");
      e.printStackTrace();
      System.exit(0);
    }
  }
}