Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;

public class Main {

    public static void main(String[] args) throws Exception {
        Connection conn = getHSQLConnection();

        DatabaseMetaData meta = conn.getMetaData();

        int txisolation = meta.getDefaultTransactionIsolation();
        System.out.println("Database default transaction isolation is " + txisolation + " ("
                + transactionIsolationToString(txisolation) + ").");
        conn.close();
    }

    /**
     * Convert a TransactionIsolation int (defined in java.sql.Connection) to
     * the corresponding printable string.
     */
    public static String transactionIsolationToString(int txisolation) {
        switch (txisolation) {
        case Connection.TRANSACTION_NONE:
            // transactions not supported.
            return "TRANSACTION_NONE";
        case Connection.TRANSACTION_READ_UNCOMMITTED:
            // All three phenomena can occur
            return "TRANSACTION_NONE";
        case Connection.TRANSACTION_READ_COMMITTED:
            // Dirty reads are prevented; non-repeatable reads and
            // phantom reads can occur.
            return "TRANSACTION_READ_COMMITTED";
        case Connection.TRANSACTION_REPEATABLE_READ:
            // Dirty reads and non-repeatable reads are prevented;
            // phantom reads can occur.
            return "TRANSACTION_REPEATABLE_READ";
        case Connection.TRANSACTION_SERIALIZABLE:
            // All three phenomena prvented; slowest!
            return "TRANSACTION_SERIALIZABLE";
        default:
            throw new IllegalArgumentException(txisolation + " not a valid TX_ISOLATION");
        }
    }

    private static Connection getHSQLConnection() throws Exception {
        Class.forName("org.hsqldb.jdbcDriver");
        String url = "jdbc:hsqldb:data/tutorial";
        return DriverManager.getConnection(url, "sa", "");
    }

}