5

I'm working on some software that sometimes needs to connect to an oracle 8.1.7 database, and sometimes to an oracle 10g database in order to perform some queries.

When connecting to the 8.1.7 database I need to use the ojdbc14.jar drivers, and the ojdbc6.jar drivers for the 10g database.

It seems that the automatic driver selection isn't smart enough to select the correct driver when both of these drivers are in the classpath, is there any way that I can specify which one is preferred in my code?

I'm not currently using any connection pool or similar abstractions, but it wouldn't be a problem to introduce something like that if needed.

Alexander Kjäll
  • 4,246
  • 3
  • 33
  • 57
  • What happens if you remove ojdbc14.jar from the classpath? Does ojdbc6.jar has legacy driver for 8.1.7? – Alexander Pogrebnyak Jan 16 '13 at 13:24
  • No, the ojdbc6.jar can't connect to 8.1.7 due to an ArrayIndexOutOfBoundsException and 8.1.7 seems to be end of life, so oracle doesn't fix bugs. But it seems that the ojdbc14.jar drivers can connect to both (interesting enough). – Alexander Kjäll Jan 16 '13 at 16:02
  • I revisited this question and realized that the version numbers are off, by ojdbc14.jar I meant 10.2.0.5.0 and by ojdbc6.jar I meant 11.2.0.3.0. – Alexander Kjäll May 29 '13 at 07:16

3 Answers3

4

If you are not using dbcp then you can do it like this

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);
        }

Note that ojdbc15.jar and ojdbc6.jar should not be on java classpath, they should be invisible for application classloader

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

You cannot decide that which driver will use at runtime using connection pool, but using connection pool(which have defination in xml format) you need not to compile your code again and again after changing the driver.

But for runtime driver selection implementation, you can create a your own a SQL helper class in which all SQL configuration and SQL related operation will be perform. In that create a method called openConnection(String driver, String username, String password), here you can decide which driver shold we use at runtime.

Amol Fasale
  • 942
  • 1
  • 10
  • 32
0

Your can create a Driver Factory Class which abstract which driver version to use

from your runtime supply the input as which type of driver you want and Factory should return that purticular type

TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57