Java JDBC Connection Create getConnection(String filename)

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

Description

A helper method to get the global default RDBMS connection.

License

Open Source License

Parameter

Parameter Description
filename a parameter

Declaration

public static Connection getConnection(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 {
    /**/* w  w w .j  a  v a2  s  .  c  o m*/
     * A helper method to get the global default RDBMS connection.
     * 
     * @param filename
     * @return
     */
    public static Connection getConnection(String filename) {
        return getDerbyConnection(filename);
    }

    public static Connection getDerbyConnection(String filename) {
        try {
            // 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);
        }
    }
}

Related

  1. getConnection(String driver, String url, String user, String pass)
  2. getConnection(String driver, String url, String username, String password)
  3. getConnection(String driverClass, String url, String user, String password)
  4. getConnection(String driverName, String urlprefix, String host, String port, String database, String user, String password)
  5. getConnection(String drvr, String url, String nm, String pass)
  6. getConnection(String jdbcDriverName, String connectionURL)
  7. getConnection(String propertiesFilePath)
  8. getConnection(String subprotocol, String subname, String user, String password)
  9. getConnection(String url, String username, String password)