Search in sources :

Example 1 with JdbcDriverClassException

use of com.varsql.core.exception.JdbcDriverClassException in project varsql by varsqlinfo.

the class JdbcDriverLoader method getJdbcDriver.

private Driver getJdbcDriver(JDBCDriverInfo jdbcDriverInfo) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    List<FileInfo> jdbcList = jdbcDriverInfo.getDriverFiles();
    if (jdbcList != null && jdbcList.size() > 0) {
        List<URL> urlList = new ArrayList<>();
        for (FileInfo item : jdbcList) {
            Resource resource = ResourceUtils.getResource(item.getPath());
            if (resource != null)
                urlList.add(resource.getURL());
        }
        if (urlList.size() > 0) {
            URLClassLoader ucl = new URLClassLoader(urlList.<URL>toArray(new URL[0]));
            Class<?> driverClass = Class.forName(jdbcDriverInfo.getDriverClass(), true, ucl);
            try {
                return (Driver) driverClass.getDeclaredConstructor().newInstance();
            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
                throw new JdbcDriverClassException(e);
            }
        // return (Driver) Class.forName(jdbcDriverInfo.getDriverClass(), true, ucl).newInstance();
        }
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) Resource(org.springframework.core.io.Resource) Driver(java.sql.Driver) URL(java.net.URL) InvocationTargetException(java.lang.reflect.InvocationTargetException) JdbcDriverClassException(com.varsql.core.exception.JdbcDriverClassException) FileInfo(com.varsql.core.common.beans.FileInfo) URLClassLoader(java.net.URLClassLoader)

Aggregations

FileInfo (com.varsql.core.common.beans.FileInfo)1 JdbcDriverClassException (com.varsql.core.exception.JdbcDriverClassException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 Driver (java.sql.Driver)1 ArrayList (java.util.ArrayList)1 Resource (org.springframework.core.io.Resource)1