Java JDBC H2 Connection getH2Connection(String dbname)

Here you can find the source of getH2Connection(String dbname)

Description

get H Connection

License

Open Source License

Declaration

private static Connection getH2Connection(String dbname) 

Method Source Code


//package com.java2s;
/*// w  ww.j  a va 2  s .  co m
 * JBoss, Home of Professional Open Source.
 * Copyright 2017, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

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

import java.sql.SQLException;

public class Main {
    private static final String DB_H2_DRIVER = "org.h2.Driver";
    private static final String DB_H2_CONNECTION = "jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1";
    private static final String DB_H2_USER = "";
    private static final String DB_H2_PASSWORD = "";

    private static Connection getH2Connection(String dbname) {
        return getConnection(DB_H2_DRIVER, DB_H2_CONNECTION, dbname, DB_H2_USER, DB_H2_PASSWORD);
    }

    public static Connection getConnection(String dbname) {
        return getH2Connection(dbname);
    }

    private static Connection getConnection(String driver, String formatUrl, String dbname, String user,
            String pass) {
        Connection dbConnection = null;
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        }
        try {
            String dbConnectionUrl = String.format(formatUrl, dbname);
            dbConnection = DriverManager.getConnection(dbConnectionUrl, user, pass);
            return dbConnection;
        } catch (SQLException e) {
            throw new IllegalStateException(
                    "Can't get connection to " + formatUrl + " database " + dbname + " of driver " + driver, e);
        }
    }
}

Related

  1. createDBWithTB(String db)
  2. deleteDatabase()
  3. deleteDatabaseH2()
  4. getConnection()
  5. getH2Connection(String filename)
  6. isH2DbAlreadyRunning()
  7. rollback(Savepoint sp)
  8. setAutoCommit(Boolean b)