DynamicDriverUtil:
package com.zy.fastdync.demos.web;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.*;
import java.util.Properties;
import java.util.logging.Logger;
public class DynamicDriverUtil {
// 动态加载jdbc驱动
public static void dynamicLoadJdbc(String inputJarPath,String className) throws Exception {
File jarPath = new File(inputJarPath);
URL u = jarPath.toURI().toURL();
URLClassLoader ucl = new URLClassLoader(new URL[]{u},Thread.currentThread().getContextClassLoader());
Driver d = (Driver) ucl.loadClass(className).newInstance();
DriverShim driver = new DriverShim(d);
DriverManager.registerDriver(driver);
}
}
class DriverShim implements Driver {
private Driver driver;
DriverShim(Driver d) {
this.driver = d;
}
public boolean acceptsURL(String u) throws SQLException {
return this.driver.acceptsURL(u);
}
public Connection connect(String u, P
文章出处登录后可见!
已经登录?立即刷新