如何从任意位置使用 JDBC 驱动程序 [英] How to use a JDBC driver from an arbitrary location

查看:17
本文介绍了如何从任意位置使用 JDBC 驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试与数据库的 JDBC 连接.执行此操作的 Java 代码应该很简单:

I need to test a JDBC connection to a database. The java code to do that should be as simple as:

DriverManager.getConnection("jdbc connection URL", "username", "password");

驱动程序管理器将为给定的连接 URL 查找适当的驱动程序.但是,我需要能够在运行时加载 JDBC 驱动程序 (jar).即,我在运行上述代码片段的 Java 应用程序的类路径上没有 JDBC 驱动程序.

The driver manager will lookup the appropriate the driver for the given connection URL. However I need to be able to load the JDBC driver (jar) at runtime. I.e I don't have the JDBC driver on the classpath of the java application that runs the snippet of code above.

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

So I can load the driver using this code, for example:

URLClassLoader classLoader = new URLClassLoader(new URL[]{"jar URL"}, this.getClass().getClassLoader());
Driver driver = (Driver) Class.forName("jdbc driver class name", true, classLoader).newInstance();

但是驱动程序管理器仍然不会选择它,因为我无法告诉它使用哪个类加载器.我尝试设置当前线程的上下文类加载器,但它仍然不起作用.

But then the driver manager still won't pick it up as I can't tell it which classloader to use. I tried setting the current thread's context classloader and it still doesn't work.

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

Anyone has any idea on the best way to achieve that?

推荐答案

来自文章 在运行时选择您的 JDBC 驱动程序;我只是把代码贴在这里供参考.

From the article Pick your JDBC driver at runtime; I am just going to post the code here for reference.

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

The idea is to trick the driver manager into thinking that the driver was loaded from the system classloader. To do this we use this class:

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 类型,它由系统类加载器加载.您现在只需使用您想要的任何类加载器加载您真正想要使用的驱动程序.例如:

This way the driver you register is of type DelegatingDriver which is loaded with the system classloader. You now just have to load the driver you really want to use using whatever classloader you want. For example:

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

这篇关于如何从任意位置使用 JDBC 驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆