Java ClassPath getDriverClassLoader(String driverClasspath)

Here you can find the source of getDriverClassLoader(String driverClasspath)

Description

Provides a ClassLoader suitable for loading SQL driver class.

License

Open Source License

Parameter

Parameter Description
driverClasspath additional classpath entries that will be used for instantiating driver class. Entries should be separated with path.separator suitable for the platform. When null or empty, driver class is expected to be already available in the ClassLoader used to load this class.

Exception

Parameter Description
MalformedURLException when provided classpath is malformed.

Return

a ClassLoader suitable for loading SQL driver class.

Declaration

public static ClassLoader getDriverClassLoader(String driverClasspath) throws MalformedURLException 

Method Source Code

//package com.java2s;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class Main {
    /**//from   w ww .  j av  a 2  s .  c om
     * Provides a ClassLoader suitable for loading SQL driver class. You need to call
     * <code>Thread.currentThread().setContextClassLoader(DataSourceFactory.getDriverClassLoader(DRIVER_CLASSPATH));</code>
     * before requesting the connection.
     * 
     * @param driverClasspath additional classpath entries that will be used for instantiating
     *        driver class. Entries should be separated with path.separator suitable for the
     *        platform. When null or empty, driver class is expected to be already available in the
     *        ClassLoader used to load this class.
     * @return a ClassLoader suitable for loading SQL driver class.
     * @throws MalformedURLException when provided classpath is malformed.
     */
    public static ClassLoader getDriverClassLoader(String driverClasspath) throws MalformedURLException {
        if (driverClasspath == null || driverClasspath.length() == 0) {
            return Thread.currentThread().getContextClassLoader();
        } else {
            String pathSeparator = System.getProperty("path.separator");
            String[] elements = driverClasspath.split(pathSeparator);
            URL[] urls = new URL[elements.length];
            for (int i = 0; i < elements.length; i++) {
                urls[i] = new URL("file", "", elements[i].trim());
            }
            return new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
        }
    }
}

Related

  1. findClassPathsToEn()
  2. findDirectoryFromClasspath(Class cls, String file)
  3. findResourceOnClasspath(String resourceName)
  4. getAvailableClassPathInfo(final ClassLoader classLoader)
  5. getBootstrapClassPath()
  6. getFileFromClasspath(final String fileName)
  7. getFileFromClasspath(String fileName)
  8. getFileFromClasspath(String name)
  9. getFileFromClasspathResource(String resource)