Java JDBC Connection Create getConnection(String driver, String connectString)

Here you can find the source of getConnection(String driver, String connectString)

Description

get Connection

License

Open Source License

Parameter

Parameter Description
driver The JDBC driver to use.
connectString The connect string to use.

Return

A new SQL connection using the specified driver & connect string

Declaration

public static Connection getConnection(String driver, String connectString) throws RemoteException 

Method Source Code

//package com.java2s;
/*//from  w  w w. j a  v  a2 s.  c o  m
Weave (Web-based Analysis and Visualization Environment)
Copyright (C) 2008-2011 University of Massachusetts Lowell
    
This file is a part of Weave.
    
Weave is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License, Version 3,
as published by the Free Software Foundation.
    
Weave 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 General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Weave.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.rmi.RemoteException;

import java.sql.Connection;

import java.sql.Driver;
import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * This maps a driver name to a Driver instance.
     * The purpose of this map is to avoid instantiating extra Driver objects unnecessarily.
     */
    private static Map<String, Driver> _driverMap = new HashMap<String, Driver>();

    /**
     * @param driver The JDBC driver to use.
     * @param connectString The connect string to use.
     * @return A new SQL connection using the specified driver & connect string 
     */
    public static Connection getConnection(String driver, String connectString) throws RemoteException {
        Connection conn = null;
        try {
            // only call newInstance once per driver
            if (!_driverMap.containsKey(driver))
                _driverMap.put(driver, (Driver) Class.forName(driver).newInstance());

            conn = DriverManager.getConnection(connectString);
        } catch (SQLException ex) {
            System.out.println(String.format("driver: %s\nconnectString: %s", driver, connectString));
            throw new RemoteException("Unable to connect to SQL database", ex);
        } catch (Exception ex) {
            throw new RemoteException("Failed to load driver: \"" + driver + "\"", ex);
        }
        return conn;
    }
}

Related

  1. getConnection(Properties prop)
  2. getConnection(Properties properties)
  3. getConnection(String dbName)
  4. getConnection(String dbUrl, String dbUser, String dbPassword)
  5. getConnection(String driver, String connectionString, String userName, String password)
  6. getConnection(String driver, String url, String user, String pass)
  7. getConnection(String driver, String url, String username, String password)
  8. getConnection(String driverClass, String url, String user, String password)
  9. getConnection(String driverName, String urlprefix, String host, String port, String database, String user, String password)