Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explicitly select jdbc driver when connecting to oracle database

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.

like image 712
Alexander Kjäll Avatar asked Jan 16 '13 12:01

Alexander Kjäll


People also ask

Which is the preferred JDBC driver type for Oracle?

If you are accessing one type of database, such as Oracle, Sybase, or IBM, the preferred driver type is type-4. If your Java application is accessing multiple types of databases at the same time, type 3 is the preferred driver.

How do I know which JDBC driver to use?

One way to check the JDBC driver version is to open the ojdbc jar file and go inside the META-INF folder, and then open the "MANIFEST. MF" file. The version can be seen next to "Specification-Version".

What is JDBC driver for Oracle?

JDBC is the Java Database Connectivity standard and it provides a mechanism for Java programs to connect to databases.To access databases using JDBC, you must use a JDBC driver. Database vendors offer JDBC drivers as free downloads. SQL Developer supports the following JDBC drivers.

How do I know if Oracle JDBC driver is installed?

You can determine the version of the JDBC driver that you installed, by calling the getDriverVersion method of the OracleDatabaseMetaData class. You can also determine the version of the JDBC driver by executing the following commands: java -jar ojdbc5. jar.


1 Answers

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

like image 114
Evgeniy Dorofeev Avatar answered Sep 25 '22 15:09

Evgeniy Dorofeev