Java 如何从任意位置使用JDBC驱动程序

Java 如何从任意位置使用JDBC驱动程序,java,jdbc,driver,Java,Jdbc,Driver,我需要测试到数据库的JDBC连接。要做到这一点,java代码应该非常简单: DriverManager.getConnection("jdbc connection URL", "username", "password"); 驱动程序管理器将为给定的连接URL查找相应的驱动程序。但是,我需要能够在运行时加载JDBC驱动程序(jar)。也就是说,运行上述代码片段的java应用程序的类路径上没有JDBC驱动程序 因此,我可以使用以下代码加载驱动程序,例如: URLClassLoader clas

我需要测试到数据库的JDBC连接。要做到这一点,java代码应该非常简单:

DriverManager.getConnection("jdbc connection URL", "username", "password");
驱动程序管理器将为给定的连接URL查找相应的驱动程序。但是,我需要能够在运行时加载JDBC驱动程序(jar)。也就是说,运行上述代码片段的java应用程序的类路径上没有JDBC驱动程序

因此,我可以使用以下代码加载驱动程序,例如:

URLClassLoader classLoader = new URLClassLoader(new URL[]{"jar URL"}, this.getClass().getClassLoader());
Driver driver = (Driver) Class.forName("jdbc driver class name", true, classLoader).newInstance();
URLClassLoader classLoader = new URLClassLoader(new URL[]{"path to my jdbc driver jar"}, this.getClass().getClassLoader());
Driver driver = (Driver) Class.forName("org.postgresql.Driver", true, classLoader).newInstance();
DriverManager.registerDriver(new DelegatingDriver(driver)); // register using the Delegating Driver

DriverManager.getDriver("jdbc:postgresql://host/db"); // checks that the driver is found
但是驱动程序管理器仍然不会接收它,因为我不能告诉它使用哪个类加载器。我试着设置当前线程的上下文类加载器,但仍然不起作用

有人知道实现这一目标的最佳方法吗?

来自文章;我只是想把代码贴在这里作为参考

其想法是欺骗驱动程序管理器,使其认为驱动程序是从系统类加载器加载的。为此,我们使用该类:

public class DelegatingDriver implements Driver
{
    private final Driver driver;

    public DelegatingDriver(Driver driver)
    {
        if (driver == null)
        {
            throw new IllegalArgumentException("Driver must not be null.");
        }
        this.driver = driver;
    }

    public Connection connect(String url, Properties info) throws SQLException
    {
       return driver.connect(url, info);
    }

    public boolean acceptsURL(String url) throws SQLException
    {
       return driver.acceptsURL(url);
    }

    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException
    {
        return driver.getPropertyInfo(url, info);
    }

    public int getMajorVersion()
    {
        return driver.getMajorVersion();
    }

    public int getMinorVersion()
    {
        return driver.getMinorVersion();
    }

    public boolean jdbcCompliant()
    { 
        return driver.jdbcCompliant();
    }
}
这样,您注册的驱动程序类型为
DelegatingDriver
,由系统类加载器加载。您现在只需使用您想要的任何类加载器来加载您真正想要使用的驱动程序。例如:

URLClassLoader classLoader = new URLClassLoader(new URL[]{"jar URL"}, this.getClass().getClassLoader());
Driver driver = (Driver) Class.forName("jdbc driver class name", true, classLoader).newInstance();
URLClassLoader classLoader = new URLClassLoader(new URL[]{"path to my jdbc driver jar"}, this.getClass().getClassLoader());
Driver driver = (Driver) Class.forName("org.postgresql.Driver", true, classLoader).newInstance();
DriverManager.registerDriver(new DelegatingDriver(driver)); // register using the Delegating Driver

DriverManager.getDriver("jdbc:postgresql://host/db"); // checks that the driver is found

问题是,
DriverManager
执行“使用直接调用方的类装入器实例的任务”。见本报告准则6-3。在这种情况下,系统类加载器并不是特别的


为了好玩,不久前我写了一篇关于这个主题的文章。我的解决方案虽然更复杂,但更完整,甚至可以从不受信任的代码中工作。另请注意
URLClassLoader.newInstance
优于
newurlclassloader

是否有充分的理由说明类路径上没有驱动程序?是的。应用程序可以连接到许多数据库。此外,由于许可证的原因,我不能捆绑所有的司机。我想要实现的是一个简单的对话框,让用户在应用程序运行时加载jar