Load Oracle JDBC Driver by reflection.
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 foundfor 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.
[1928 byte] By [
dfundaka] at [2007-11-27 11:24:00]

# 2
> 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.
# 6
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
> 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
> 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.