当前位置: 首页 > 面试题库 >

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

於乐
2023-03-14
问题内容

我需要测试与数据库的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();

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

有人对实现这一目标的最佳方法有任何想法吗?


问题答案:

从文章中选择运行时的JDBC驱动程序;我将在此处发布代码以供参考。

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

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[]{"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


 类似资料:
  • 每当我试图使用ODBC驱动程序访问MDB文件时,它都会给我一个错误: 因此,我决定使用UCanAccess JDBC驱动程序。

  • 当我将mysql jdbc驱动程序复制到JDK的\jre\lib\ext时,它执行得非常好。现在,我想通过指定环境变量的类路径来使用jdbc。但是,这样做之后,我的程序抛出异常: “java.sql.SQLException:找不到适合jdbc的驱动程序:mysql://localhost/books" 如何设置类路径?

  • 问题内容: 我想从MySQL中的表中获取数据并在TextView中显示它,但是当我尝试与数据库连接时遇到了一些问题。 我正在使用Eclipse for Android,当我尝试从Java Project中的MySQL获取数据时,它可以工作,但是当我使用Android Project时,则无法工作。 有谁知道如何使用MySQL JDBC驱动程序将MySQL与Android Project连接? 或者

  • 问题内容: 我有一种方法可以将记录插入Postgres DB中,并返回为所述记录生成的标识字段。问题是,如果我在我的POM文件中包含Redshift驱动程序,那么该驱动程序将代替Postgres驱动程序被使用- Redshift驱动程序不允许返回标识值。 代码是: 使用此POM时,它可以工作: 使用此POM时,它不起作用: 是什么让Java选择Redshift驱动程序而不是Postgres驱动程序

  • 我是卡桑德拉和莫文的新手。我试图在eclipse中编写一个简单的java程序,它使用cassandra java驱动程序连接到我设置的cassandra节点。我找到了这个存储库https://github.com/datastax/java-driver但我不知道该怎么处理它。有谁能给我一步一步的说明来获取驱动程序并创建一个使用驱动程序的简单eclipse项目吗。

  • 如何将Crystal Reports与ucanaccess jdbc驱动程序一起使用?我得到“未找到驱动程序”错误。我将ucanaccess jar文件复制到程序文件(C:\program files(x86)\Business objects\common\3.5\java)中的业务对象,编辑了crconfig.xml文件,并且已经在Crystal Reports中配置了JDBC(JNDI)。M