Here you can find the source of getConnection(String driver, String connectString)
Parameter | Description |
---|---|
driver | The JDBC driver to use. |
connectString | The connect string to use. |
public static Connection getConnection(String driver, String connectString) throws RemoteException
//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; } }