简体   繁体   中英

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

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();

Try the following method:

conn.getMetaData().getDriverName();

Couple of other methods available:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM