Java JDBC Driver createDriver(String driverClassName)

Here you can find the source of createDriver(String driverClassName)

Description

create Driver

License

Apache License

Declaration

public static Driver createDriver(String driverClassName) throws SQLException 

Method Source Code

//package com.java2s;
/*//  w w  w  .ja  v  a  2s  .  c om
 * Copyright 1999-2011 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.sql.Driver;

import java.sql.SQLException;

public class Main {
    public static Driver createDriver(String driverClassName) throws SQLException {
        return createDriver(null, driverClassName);
    }

    public static Driver createDriver(ClassLoader classLoader, String driverClassName) throws SQLException {
        if (classLoader != null) {
            try {
                return (Driver) classLoader.loadClass(driverClassName).newInstance();
            } catch (IllegalAccessException e) {
                throw new SQLException(e.getMessage(), e);
            } catch (InstantiationException e) {
                throw new SQLException(e.getMessage(), e);
            } catch (ClassNotFoundException e) {
                throw new SQLException(e.getMessage(), e);
            }
        }

        try {
            return (Driver) Class.forName(driverClassName).newInstance();
        } catch (IllegalAccessException e) {
            throw new SQLException(e.getMessage(), e);
        } catch (InstantiationException e) {
            throw new SQLException(e.getMessage(), e);
        } catch (ClassNotFoundException e) {
            // skip
        }

        try {
            return (Driver) Thread.currentThread().getContextClassLoader().loadClass(driverClassName).newInstance();
        } catch (IllegalAccessException e) {
            throw new SQLException(e.getMessage(), e);
        } catch (InstantiationException e) {
            throw new SQLException(e.getMessage(), e);
        } catch (ClassNotFoundException e) {
            throw new SQLException(e.getMessage(), e);
        }
    }
}

Related

  1. createDriver(ClassLoader classLoader, String driverClassName)
  2. createDriver(String driverClassName)
  3. createDriver(String driverClassName)
  4. getDriverClassName(String rawUrl)
  5. getDriverClassName(String rawUrl)
  6. getDriverFromPath(String path, String className)