mmuzahid
mmuzahid

Reputation: 2270

How to get JDBC Driver class name from connection object during runtime?

I have created my JDBC connection like bellow:

java.sql.Connection conn =  java.sql.DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:DBSID", "DB_USER",
                    "DB_PASS");

How can I get JDBC driver class name (e.g. "oracle.jdbc.driver.OracleDriver") during run-time something like that:

String driverClsName  = getDriverClsName(conn);
//e.g.  driverClsName = "oracle.jdbc.driver.OracleDriver"

I've already searched stackoverflow for that but no final solution found.

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

What should be code inside below method:

String getDriverClsName(Connection conn){
    //code here
}

I've already tried conn.getMetaData().getDriverName(); which is returning Oracle JDBC driver.

My requirement is to show information(such as class name) about current driver being used in the application.

Upvotes: 2

Views: 7051

Answers (2)

user330315
user330315

Reputation:

DriverManager.getDriver(String) should do this:

Connection conn = DriverManager.getConnection(...); 

String originalURL = conn.getMetaData().getURL();
Driver drv = DriverManager.getDriver(originalURL);
String driverClass = drv.getClass().getName();

Upvotes: 4

RAS
RAS

Reputation: 8158

Try the following method:

conn.getMetaData().getDriverName();

Couple of other methods available:

conn.getMetaData().getDriverVersion();
conn.getMetaData().getDriverMajorVersion();
conn.getMetaData().getDriverMinorVersion();

Upvotes: 3

Related Questions