Java JDBC Oracle Connection getConnection(String connectString)

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

Description

get Connection

License

Open Source License

Parameter

Parameter Description
connectString The connect string to use.

Return

A new SQL connection using the specified driver & connect string

Declaration

public static Connection getConnection(String connectString)
        throws RemoteException 

Method Source Code

//package com.java2s;
/* ***** BEGIN LICENSE BLOCK *****
 *
 * This file is part of Weave.//w  ww.  j a  v  a2s .c o m
 *
 * The Initial Developer of Weave is the Institute for Visualization
 * and Perception Research at the University of Massachusetts Lowell.
 * Portions created by the Initial Developer are Copyright (C) 2008-2015
 * the Initial Developer. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 * ***** END LICENSE BLOCK ***** */

import java.rmi.RemoteException;

import java.sql.Connection;

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

import java.sql.SQLException;

import java.util.HashMap;

public class Main {
    public static String MYSQL = "MySQL";
    public static String SQLITE = "SQLite";
    public static String POSTGRESQL = "PostgreSQL";
    public static String SQLSERVER = "Microsoft SQL Server";
    public static String ORACLE = "Oracle";
    /**
     * This maps a driver name to a Driver instance.
     * The purpose of this map is to avoid instantiating extra Driver objects unnecessarily.
     */
    private static HashMap<String, Driver> _driverMap = new HashMap<String, Driver>();

    /**
     * @param connectString The connect string to use.
     * @return A new SQL connection using the specified driver & connect string 
     */
    public static Connection getConnection(String connectString)
            throws RemoteException {
        return getConnection(connectString, null, null);
    }

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

            if (user == null && pass == null)
                conn = DriverManager.getConnection(connectString);
            else
                conn = DriverManager.getConnection(connectString, user,
                        pass);
        } catch (SQLException ex) {
            System.err
                    .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;
    }

    /**
     * @return A driver name that can be used in the getConnection() function.
     */
    public static String getDriverFromConnectString(String connectString)
            throws RemoteException {
        if (connectString.startsWith("jdbc:sqlserver"))
            return "com.microsoft.sqlserver.jdbc.SQLServerDriver";

        String dbms = getDbmsFromConnectString(connectString);
        if (dbms.equalsIgnoreCase(MYSQL))
            return "com.mysql.jdbc.Driver";
        if (dbms.equalsIgnoreCase(SQLITE))
            return "org.sqlite.JDBC";
        if (dbms.equalsIgnoreCase(POSTGRESQL))
            return "org.postgis.DriverWrapper";
        if (dbms.equalsIgnoreCase(SQLSERVER))
            return "net.sourceforge.jtds.jdbc.Driver";
        if (dbms.equalsIgnoreCase(ORACLE))
            return "oracle.jdbc.OracleDriver";

        throw new RemoteException("Unknown DBMS");
    }

    public static String getDbmsFromConnectString(String connectString)
            throws RemoteException {
        if (connectString.startsWith("jdbc:jtds")
                || connectString.startsWith("jdbc:sqlserver"))
            return SQLSERVER;
        if (connectString.startsWith("jdbc:oracle"))
            return ORACLE;
        if (connectString.startsWith("jdbc:mysql"))
            return MYSQL;
        if (connectString.startsWith("jdbc:sqlite"))
            return SQLITE;
        if (connectString.startsWith("jdbc:postgresql"))
            return POSTGRESQL;

        throw new RemoteException("Unknown DBMS");
    }
}

Related

  1. getConnection()
  2. getConnection()
  3. getConnection()
  4. getConnection()
  5. getConnection()
  6. getConnection(String dbType, String dbIp, String dbPort, String dbName, String userName, String password)
  7. getConnection(String ip, String serviceName, String uid, String pwd)
  8. getDatabaseConnection()
  9. getDBVersion(String dbSourceName, String server, int port, String dbName, String userid, String pwd)