Java JDBC Derby Connection getDerbyConnection(String filename)

Here you can find the source of getDerbyConnection(String filename)

Description

get Derby Connection

License

Open Source License

Declaration

public static Connection getDerbyConnection(String filename) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {
    public static Connection getDerbyConnection(String filename) {
        try {//  ww w .ja  v a 2s. co m
            // Check if the database exists. If it doesn't, append ;create=true to
            // the URL.
            File db = new File(filename);
            boolean exists = false;

            if (!db.exists()) {
                // Check for files that have the same prefix in this directory
                for (File child : db.getParentFile().listFiles()) {
                    if (child.getName().startsWith(db.getName())) {
                        exists = true;
                    }
                }
            }

            System.setProperty("derby.stream.error.file", "error.txt");
            System.setProperty("derby.system.home", db.getParent());

            // Largest page size Derby allows. This will reduce the number of I/O 
            // operations and speed things up a bit.
            System.setProperty("derby.storage.pageSize", "32768");
            // Larger page cache will speed things up. TODO: determine this dynamically
            // from Runtime.getRuntime().
            // 32KB page size / 10K cache size ~= 312MB of memory (not including 
            // overhead).
            //System.setProperty("derby.storage.pageCacheSize", "10000");

            Class.forName("org.apache.derby.jdbc.EmbeddedDriver")
                    .newInstance();
            String url = String.format("jdbc:derby:%s",
                    db.getAbsolutePath());

            if (!exists) {
                url = url.concat(";create=true");
            }

            return DriverManager.getConnection(url);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Failed to load Derby db driver!", e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException("Failed to load Derby db driver!", e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Failed to load Derby db driver!", e);
        }
    }

    /**
     * A helper method to get the global default RDBMS connection.
     * 
     * @param filename
     * @return
     */
    public static Connection getConnection(String filename) {
        return getDerbyConnection(filename);
    }
}

Related

  1. existsInSessionTable(String id, boolean verbose)
  2. getConnection()
  3. getConnection()
  4. getDatabaseConnection()
  5. getDefaultDerbyConnection()
  6. getDerbyDatabaseProperty(Statement derbyStatementForQuerying, String propertyKey)
  7. getDerbyUnitConnection()
  8. getDriver(String className)
  9. getLocalConnection()