Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get driver class name (not driver name) from jdbc connection

I have a context.xml file in the below format

<Context shallowOutput="true" path="/">
<WatchedResource>WEB-INF/web.xml</WatchedResource>

  <Resource name="jdbc/myDataSource"
        auth="Container"
        type="javax.sql.DataSource"
        factory="org.apache.commons.dbcp.BasicDataSourceFactory"
        driverClassName="oracle.jdbc.driver.OracleDriver"
        username="OMITTED"
        password="OMITTED"
        url="OMITTED"
        maxActive="20"
        maxIdle="10"
        maxWait="-1"/>

From this contex.xml I need to get my Driver CLASS name.

Everytime I try like

DataSource ds = (DataSource)context.lookup("java:/jdbc/myDataSource")

and try to like get the the Driver Class name from the connection using

ds.getConnection().getMetatData().getDriverName()

It is returning just Oracle JDBC Driver instead of the class name oracle.jdbc.driver.OracleDriver

How can I get the class name from the context.

like image 598
Nigel Thomas Avatar asked Mar 11 '14 05:03

Nigel Thomas


People also ask

How do I know my JDBC driver?

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".

Why do we write class forName () in JDBC?

Use the Class. forName() method to load the driver. The forName() method dynamically loads a Java class at runtime. When an application calls the forName() method, the Java Virtual Machine (JVM) attempts to find the compiled form (the bytecode) that implements the requested class.


1 Answers

I think the best you can hope for is:

DriverManager.getDriver(ds.getConnection().getMetaData().getURL()).getClass();

The metadata should return the URL for this connection and the URL prefix should be registered with the DriverManager (uniquely).

like image 142
Jason Pyeron Avatar answered Sep 27 '22 17:09

Jason Pyeron