Nigel Thomas
Nigel Thomas

Reputation: 1929

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.

Upvotes: 10

Views: 38762

Answers (4)

user1767316
user1767316

Reputation: 3631

Using Tomcat (7) this works:

if(source instanceof org.apache.tomcat.dbcp.dbcp.BasicDataSource){
    logger.info("Driver className: "+((org.apache.tomcat.dbcp.dbcp.BasicDataSource)source).getDriverClassName());
}

You will also need to include the dbcp library at build time:

<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-dbcp -->
<dependency><!-- to check driver name effectively running -->
        <scope>provided</scope>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-dbcp</artifactId>
        <version>7.0.47</version>
</dependency>

Upvotes: -1

Jason Pyeron
Jason Pyeron

Reputation: 2528

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

Upvotes: 10

nico
nico

Reputation: 460

I use a "try" algorithm based on reflection. OracleDataSource contains the driver in a "driver" attribute, and there may be lots of DataSource that does the same. So the following :

Field field = dataSource.getClass().getDeclaredField("driver");
field.setAccessible(true);
return field.get(dataSource).getClass().getName();

do the job.

Upvotes: 0

Michał Niklas
Michał Niklas

Reputation: 54302

For any object you can use object.getClass().getName()

For JDBC connection it looks like:

String db_class = DriverManager.getConnection(db_url, usr, passwd).getClass().getName();

For my PostgreSQL driver it returns:

org.postgresql.jdbc4.Jdbc4Connection

In your code this should work:

ds.getConnection().getClass().getName()

And simple procedure that shows class name of connection:

public static void show_connection_info(Connection conn)
    {
    System.out.println("Connection: " + conn);
    System.out.println("Connection class: " + conn.getClass());
    System.out.println("Connection class name: " + conn.getClass().getName());
    }

For Oracle connection I used in test I got:

Connection: oracle.jdbc.driver.T4CConnection@1e1c66a
Connection class: class oracle.jdbc.driver.T4CConnection
Connection class name: oracle.jdbc.driver.T4CConnection

Upvotes: 4

Related Questions