Java JDBC Driver createDriver(ClassLoader classLoader, String driverClassName)

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

Description

create Driver

License

Open Source License

Declaration

public static Driver createDriver(ClassLoader classLoader, String driverClassName) throws SQLException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.sql.Driver;
import java.sql.SQLException;

public class Main {
    public static Driver createDriver(String driverClassName) throws SQLException {
        return createDriver(null, driverClassName);
    }//from  ww  w .ja va2 s . co m

    public static Driver createDriver(ClassLoader classLoader, String driverClassName) throws SQLException {
        Class<?> clazz = null;
        if (classLoader != null) {
            try {
                clazz = classLoader.loadClass(driverClassName);
            } catch (ClassNotFoundException e) {
                // swallow it
            }
        }
        if (clazz == null) {
            try {
                ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
                if (contextLoader != null) {
                    clazz = contextLoader.loadClass(driverClassName);
                }
            } catch (ClassNotFoundException e) {
                // swallow it
            }
        }
        if (clazz == null) {
            try {
                clazz = Class.forName(driverClassName);
            } catch (ClassNotFoundException e) {
                throw new SQLException(e.getMessage(), e);
            }
        }
        try {
            return (Driver) clazz.newInstance();
        } catch (IllegalAccessException e) {
            throw new SQLException(e.getMessage(), e);
        } catch (InstantiationException e) {
            throw new SQLException(e.getMessage(), e);
        }
    }
}

Related

  1. createDriver(String driverClassName)
  2. createDriver(String driverClassName)
  3. createDriver(String driverClassName)
  4. getDriverClassName(String rawUrl)