在一个 Java 应用程序中使用多个 Oracle JDBC 驱动程序? [英] Using Multiple Oracle JDBC drivers in one Java application?

查看:24
本文介绍了在一个 Java 应用程序中使用多个 Oracle JDBC 驱动程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 JDBC 连接到两个不同的 Oracle 数据库(一个是 8.0.5.0.0,一个是 12c).我确实有两个 JDBC 驱动程序,它们可以通过简单的hello world"应用程序单独并成功地连接到相应的数据库.下面,我将它们放在一个 Java 应用程序中,不幸的是它不再工作(两个驱动程序都已加载).

I want to connect to two different Oracle databases (one 8.0.5.0.0 and one 12c) via JDBC. I do have both JDBC drivers that can individually and successfully connect to the corresponding DB via simple "hello world" applications. Below, I have put both of them together into one Java application, which unfortunately does not work anymore (with both drivers being loaded).

我已阅读这篇文章:处理来自同一供应商的多个 JDBC 驱动程序.选项 1 提到可能有办法,但似乎存在一个主要问题:

I have read this post: Handle multiple JDBC drivers from the SAME VENDOR . The option 1 mentioned there might be the way to go, but there seems to be one major problem:

似乎OracleDataSource在旧版本8驱动中还不存在,只有在以后的版本中才引入(在12c版本驱动中存在).

It seems that OracleDataSource does not yet exist in the old version 8 driver and only has been introduced at later versions (in the 12c version driver it exists).

关于如何使用一个 Java 应用程序和两个 JDBC 驱动程序连接到这两个 Oracle 数据库的任何提示?

Any hints, on how to connect to these two Oracle databases with one single Java application and two JDBC drivers?

import java.sql.*;

class db {
    public static void main (String args []) throws SQLException {

        // Oracle 8 connection
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        Connection c1 = DriverManager.getConnection(
                "jdbc:oracle:thin:@some-oracle-8-server:port:sid",
                "my-user",
                "my-password");
        Statement s1 = c1.createStatement ();
        ResultSet r1 = s1.executeQuery ("SELECT banner FROM V$VERSION WHERE banner LIKE 'Oracle%'");
        while (r1.next ()) {
            System.out.println(r1.getString (1));
        }
        c1.close();

        // Oracle 12 connection
        Connection c2 = DriverManager.getConnection(
                "jdbc:oracle:thin:@some-oracle-12-server:port:sid",
                "my-user",
                "my-password");
        Statement s2 = c2.createStatement ();
        ResultSet r2 = s2.executeQuery ("SELECT banner FROM V$VERSION WHERE banner LIKE 'Oracle%'");
        while (r2.next ()) {
            System.out.println(r2.getString (1));
        }
        c2.close();
    }
}

提前感谢!

推荐答案

如果您不注册驱动程序,就可以避免它们被同一个类加载器加载.

If you don't register the drivers you avoid them being loaded by the same classloader.

然后您可以使用两个不同的驱动程序通过单独的类加载器加载它们来创建连接:

Then you can create connections using the two different drivers by loading them through separate classloaders:

// Oracle 8 connection
File jar = new File("/path/to/oracle8.jar");
URL[] cp = new URL[1];
cp[0] = jar.toURI().toURL();
URLClassLoader ora8loader = new URLClassLoader(cp, ClassLoader.getSystemClassLoader());
Class drvClass = ora8loader.loadClass("oracle.jdbc.driver.OracleDriver");
Driver ora8driver = (Driver)drvClass.newInstance();

Properties props = new Properties();
// "user" instead of "username"
props.setProperty("user", "my-user");
props.setProperty("password", "my-password");
Connection ora8conn = ora8driver.connect("jdbc:oracle:thin:@some-oracle-8-server:port:sid",props);

然后对 Oracle 12 驱动程序执行相同操作.

Then do the same for the Oracle 12 driver.

可能还可以通过 DriverManager 继续使用其他"驱动程序,但我不确定这一点.

You might also be able to still use the "other" driver through DriverManager, but I'm not sure about that.

在某些极端情况下,访问 Oracle 特定类会变得有点复杂,但总的来说,您可以毫无问题地使用通过此方法创建的连接.

There are some corner cases where accessing Oracle specific classes gets a bit complicated, but in general you can use the connections created through this without any problems.

这篇关于在一个 Java 应用程序中使用多个 Oracle JDBC 驱动程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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