连接到oracle数据库时显式选择jdbc驱动程序

Ale*_*äll 5 java jdbc oracle10g oracle8i

我正在研究一些有时需要连接到oracle 8.1.7数据库的软件,有时需要连接到oracle 10g数据库才能执行一些查询.

连接到8.1.7数据库时,我需要使用ojdbc14.jar驱动程序和10g数据库的ojdbc6.jar驱动程序.

当这两个驱动程序都在类路径中时,似乎自动驱动程序选择不够智能,无法选择正确的驱动程序,有什么方法可以指定我的代码中哪一个是首选的?

我目前没有使用任何连接池或类似的抽象,但如果需要引入类似的东西也不会有问题.

Evg*_*eev 4

如果你不使用 dbcp 那么你可以这样做

class Test 
        static Driver driver5;
        static Driver driver6;

        static void init() throws Exception {
            ClassLoader cl5 = new URLClassLoader(new URL[] { new URL("file:lib/ojdbc15.jar") });
            driver5 = (Driver) cl5.loadClass("oracle.jdbc.driver.OracleDriver").newInstance();
            ClassLoader cl6 = new URLClassLoader(new URL[] { new URL("file:lib/ojdbc6.jar") });
            driver6 = (Driver) cl6.loadClass("oracle.jdbc.driver.OracleDriver").newInstance();
        }

        public static void main(String[] args) throws Exception {
            Properties props = new Properties();
            props.put("user", "user");
            props.put("password", "pwd");
            String url = "jdbc:oracle:thin:@host:1529:sid";
            Connection conn5 = driver5.connect(url, props);
            Connection conn6 = driver6.connect(url, props);
        }
Run Code Online (Sandbox Code Playgroud)

请注意,ojdbc15.jar 和 ojdbc6.jar 不应该位于 java 类路径上,它们对于应用程序类加载器应该是不可见的