14 Replies Latest reply on Aug 7, 2007 5:13 PM by jschellSomeoneStoleMyAlias

    Load Oracle JDBC Driver by reflection.

    843859
      Hi!
      I'm trying to load driver by reflection like this,
      try {
        URL ora8libURL = new URL("file:///C:/oracle/product/10.2.0/clientDev/jdbc/lib/classes12.jar");
        Class.forName("oracle.jdbc.OracleDriver", true, URLClassLoader.newInstance(new URL[]{ora8libURL}));
        
          Enumeration<Driver> drivers = DriverManager.getDrivers();
          while(drivers.hasMoreElements())
          {
            Driver drv = drivers.nextElement();
            System.out.println(drv.getClass().getName());
          }
      
        conn = DriverManager.getConnection("jdbc:oracle:thin:@//192.168.186.13:1521/rdb817","user","password");
      }
      catch(ClassNotFoundException ex) {ex.printStackTrace();}
      catch(MalformedURLException ex) {ex.printStackTrace();}
      catch (SQLException ex) {ex.printStackTrace();}
      but i've got this error:
      sun.jdbc.odbc.JdbcOdbcDriver
      
      java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@//192.168.186.13:1521/rdb817
           at java.sql.DriverManager.getConnection(Unknown Source)
           at java.sql.DriverManager.getConnection(Unknown Source)
      
      ...
      However, when i specify driver explicit in classpath it works fine.

      Help.
        • 1. Re: Load Oracle JDBC Driver by reflection.
          843859
          Sorry, your way off. Forget everyting your trying to do. Put classes12.jar in your project's lib folder add the jar file to the classpath. Look online for examples of creating a connection pool.
          • 2. Re: Load Oracle JDBC Driver by reflection.
            jschellSomeoneStoleMyAlias
            Sorry, your way off. Forget everyting your trying to
            do. Put classes12.jar in your project's lib folder
            add the jar file to the classpath. Look online for
            examples of creating a connection pool.
            I doubt that is what the OP is intending.
            • 3. Re: Load Oracle JDBC Driver by reflection.
              jschellSomeoneStoleMyAlias
              I'm trying to load driver by reflection like this,
              Why?

              Do you understand that the code you posted will not allow you to unload a driver? Is that something that you need to do?
              • 4. Re: Load Oracle JDBC Driver by reflection.
                DrClap
                However, when i specify driver explicit in classpath it works fine.

                Help.
                So you have a working solution, then. What's the reason for trying to do it some other way?
                • 5. Re: Load Oracle JDBC Driver by reflection.
                  800487
                  No suitable driver found for jdbc:oracle:thin:@//192.168.186.13:1521/rdb817
                  exception is generated either if the connection url is wrong or the driver class could not be loaded, which could be if the driver class is not in the classpath.
                  • 6. Re: Load Oracle JDBC Driver by reflection.
                    843859
                    I've faced problem while working with application that access different versions of Oracle(10gR2 and old one 8.0.5).

                    There is problem wile connecting to old 8.0.5 Oracle with driver version prior to 8.0.17.
                    So i need to use two different drivers: for 10g and 8.05.

                    I have 2 jdbc libraries: classes12.zip(Oracle 8.0.5) and classes12.jar(Oracle 10g).
                    In both libraries there is class oracle.jdbc.driver.OracleDriver.

                    And i have a config file where i specify datasources looks like this:
                    <ds-config>
                      <datasource name="ora805">
                      <driver>oracle.jdbc.driver.OracleDriver</driver>
                      <url>...
                      <version>8.0.5</version>
                      ...
                      </datasource>
                      <datasource name="ora10g">
                      ...
                      <version>10.0.2</version>
                      ..
                      </datasource>
                    </ds-config>
                    The problem is to load necessary jdbc driver by reflection as specified in config file.
                    • 7. Re: Load Oracle JDBC Driver by reflection.
                      jschellSomeoneStoleMyAlias
                      No suitable driver found for
                      jdbc:oracle:thin:@//192.168.186.13:1521/rdb817

                      exception is generated either if the connection url
                      is wrong or the driver class could not be loaded,
                      That would result in a class not found exception.
                      • 8. Re: Load Oracle JDBC Driver by reflection.
                        jschellSomeoneStoleMyAlias
                        I've faced problem while working with application
                        that access different versions of Oracle(10gR2 and
                        old one 8.0.5).

                        There is problem wile connecting to old 8.0.5 Oracle
                        with driver version prior to 8.0.17.
                        So i need to use two different drivers: for 10g and
                        8.05.

                        I have 2 jdbc libraries: classes12.zip(Oracle 8.0.5)
                        and classes12.jar(Oracle 10g).
                        In both libraries there is class
                        oracle.jdbc.driver.OracleDriver.
                        Presumably because different pieces of code are accessing this rather than because you are mixing different connections in the same code.

                        To do this you are going to need to use custom class loaders because there is sure to be name collisions otherwise.

                        Using custom class loaders with jdbc drivers is not simple. You will have to search in this forum for how to use them.

                        Note as well that you need to deal with the same problems that class loaders bring in general - in particular you can't use any thing specific to Oracle in your code unless that code exists in the same class loader or you rely entirely on reflection calls.
                        • 9. Re: Load Oracle JDBC Driver by reflection.
                          843859
                          I had the same problem you have, until i read your post :-).
                          Now, with your help, i found a solution.

                          The problem with your code is that the DriverManager that you are using doesn't "see" the driver you are loading. But you can create the driver instance directly:
                          /*
                           * Testes.java
                           *
                           * Created on 31 de Julho de 2007, 15:04
                           *
                           * To change this template, choose Tools | Template Manager
                           * and open the template in the editor.
                           */
                          
                          
                          
                          import java.sql.*;
                          import java.net.*;
                          import java.util.*;
                          
                          /**
                           *
                           * @author paulo
                           */
                          public class Testes {
                              
                              /** Creates a new instance of Testes */
                              public Testes() {
                              }
                              
                              /**
                               * @param args the command line arguments
                               */
                              public static void main(String[] args) {
                                  // TODO code application logic here
                                  
                                  String dbUrl10_1 = "jdbc:oracle:thin:@(DESCRIPTION="
                                                  + "(ADDRESS_LIST="
                                                  + "     (ADDRESS="
                                                  + "          (PROTOCOL=TCP)"
                                                  + "          (HOST=ffcluster01-vip)"
                                                  + "          (PORT=1521)"
                                                  + "     )"
                                                  + "     (ADDRESS="
                                                  + "          (PROTOCOL=TCP)"
                                                  + "          (HOST=ffcluster02-vip)"
                                                  + "          (PORT=1521)"
                                                  + "     )"
                                                  + "     (LOAD_BALANCE=yes)"
                                                  + ")"
                                                  + "(CONNECT_DATA="
                                                  + "     (SERVICE_NAME=bh.hdff.pt)"
                                                  + "     (FAILOVER_MODE ="
                                                  + "          (TYPE = SELECT)"
                                                  + "          (METHOD = BASIC)"
                                                  + "          (RETRIES = 180)"
                                                  + "          (DELAY = 5)"
                                                  + "     )"
                                                  + ")"
                                                  + ")";
                                  
                                  String dbUrl10_2 = "jdbc:oracle:thin:@ffcluster01:1521:BH1";
                          
                                  String dbUrl7 = "jdbc:oracle:thin:@7.30.5.1:1521:HFF";
                                  
                                  try {
                                       
                                       //Class.forName("oracle.jdbc.OracleDriver");
                                       
                                      URL ora7libURL = new URL("file:///C:\\PAULO\\aplicacoes\\ojdbc14.jar");
                                      URL ora10libURL = new URL("file:///C:\\PAULO\\aplicacoes\\ojdbc14_10g.jar");
                                      
                                      Driver drv10 = (Driver)(Class.forName("oracle.jdbc.OracleDriver", true, URLClassLoader.newInstance(new URL[]{ora10libURL}))).newInstance();
                                      System.out.println("Versao 10: "+drv10.getMajorVersion()+"."+drv10.getMinorVersion()+"\n");
                          
                                      Driver drv7 = (Driver)(Class.forName("oracle.jdbc.OracleDriver", true, URLClassLoader.newInstance(new URL[]{ora7libURL}))).newInstance();
                                      System.out.println("Versao 7: "+drv7.getMajorVersion()+"."+drv7.getMinorVersion()+"\n");
                          
                                      Enumeration drivers = DriverManager.getDrivers();
                                      while(drivers.hasMoreElements()) {
                                          Driver drv = (Driver)drivers.nextElement();
                                          System.out.println(drv.getClass().getName());
                                          System.out.println("Versao: "+drv.getMajorVersion()+"."+drv.getMinorVersion()+"\n");
                                      }
                                      System.out.flush();
                          
                                      Properties prt = new Properties();
                                      prt.setProperty("user", "****");
                                      prt.setProperty("password", "*****");
                                      
                                      Connection conn = drv10.connect(dbUrl10_1,prt);
                                      conn.close();
                          
                          
                                      prt = new Properties();
                                      prt.setProperty("user", "****");
                                      prt.setProperty("password", "****");
                                      
                                      conn = drv7.connect(dbUrl7,prt);
                                      conn.close();
                          
                                  } catch(ClassNotFoundException ex) {
                                      ex.printStackTrace();
                                  } catch(MalformedURLException ex) {
                                      ex.printStackTrace();
                                  } catch(SQLException ex) {
                                      ex.printStackTrace();
                                  } catch(Exception ex) {
                                      ex.printStackTrace();
                                  }
                                  
                                  
                                  
                              }
                              
                          }
                          Has you can see, the DriverManager doesn't know about the divers loaded with the new ClassLoader.

                          The only thing you have to pay attention is not to have the classes for "oracle.jdbc.OracleDriver" in your system classpath.
                          • 10. Re: Load Oracle JDBC Driver by reflection.
                            843859
                            for oracle thin driver I use following URL format and It works
                            "jdbc:oracle:thin:@Host:Port:ServiceID"
                            • 11. Re: Load Oracle JDBC Driver by reflection.
                              jschellSomeoneStoleMyAlias
                              for oracle thin driver I use following URL format and
                              It works

                              "jdbc:oracle:thin:@Host:Port:ServiceID"
                              It works by loading different versions of the oracle driver into the same application/VM?
                              • 12. Re: Load Oracle JDBC Driver by reflection.
                                843859
                                I have used this syntex with JDBC drivers that came with oracle 9iand 10g. I havent used it with any older versions. I think the OP it trying to load an older JDBC driver
                                • 13. Re: Load Oracle JDBC Driver by reflection.
                                  843859
                                  In my case, my application needs to connect to Oracle 7 and Oracle 10g RAC.

                                  The Oracle 10g JDBC driver doesn't connect to Oracle 7.
                                  The Oracle 9i JDBC driver (the driver i was using to connect to Oracle 7 previously ) connects to both Oracle 7 and Oracle 10g but i can only connect to one node of the RAC at a time using the connection string:
                                  jdbc:oracle:thin:@ffcluster01:1521:BH1
                                  or
                                  jdbc:oracle:thin:@ffcluster02:1521:BH2
                                  If i want to connect to the cluster, and not only one of the nodes, i must use:
                                  jdbc:oracle:thin:@(DESCRIPTION=
                                       (ADDRESS_LIST=
                                            (ADDRESS=
                                                 (PROTOCOL=TCP)
                                                 (HOST=ffcluster01-vip)
                                                 (PORT=1521)
                                            )
                                            (ADDRESS=
                                                 (PROTOCOL=TCP)
                                                 (HOST=ffcluster02-vip)
                                                 (PORT=1521)
                                            )
                                            (LOAD_BALANCE=yes)
                                       )
                                       (CONNECT_DATA=
                                            (SERVICE_NAME=bh.hdff.pt)
                                            (FAILOVER_MODE =
                                                 (TYPE = SELECT)
                                                 (METHOD = BASIC)
                                                 (RETRIES = 180)
                                                 (DELAY = 5)
                                            )
                                       )
                                  )
                                  and the driver that connects to Oracle 7 doesn't accept this connect string.

                                  So i needed to load both versions of the driver in the same JVM.
                                  I accomplish that with the code of my previus post.
                                  • 14. Re: Load Oracle JDBC Driver by reflection.
                                    jschellSomeoneStoleMyAlias
                                    I have used this syntex with JDBC drivers that came
                                    with oracle 9iand 10g. I havent used it with any
                                    older versions. I think the OP it trying to load an
                                    older JDBC driver
                                    You misunderstood the question.

                                    The OP wants to load two different drivers at the same time.