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

Nigel Thomas picture Nigel Thomas · Mar 11, 2014 · Viewed 25.4k times · Source

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.

Answer

Jason Pyeron picture Jason Pyeron · Oct 21, 2015

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