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

查看:149
本文介绍了如何从任意位置使用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天全站免登陆