简体   繁体   English

如何从jdbc连接获取驱动程序类名(不是驱动程序名)

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

I have a context.xml file in the below format 我有一个以下格式的context.xml文件

<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. 从这个contex.xml我需要获取我的Driver CLASS名称。

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 它只返回Oracle JDBC Driver而不是类名oracle.jdbc.driver.OracleDriver

How can I get the class name from the context. 如何从上下文中获取类名。

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). 元数据应返回此连接的URL,URL前缀应使用DriverManager注册(唯一)。

For any object you can use object.getClass().getName() 对于任何对象,您可以使用object.getClass().getName()

For JDBC connection it looks like: 对于JDBC连接,它看起来像:

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

For my PostgreSQL driver it returns: 对于我的PostgreSQL驱动程序,它返回:

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: 对于我在测试中使用的Oracle连接,我得到了:

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

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. OracleDataSource包含“driver”属性中的驱动程序,并且可能有许多DataSource执行相同操作。 So the following : 所以以下内容:

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

do the job. 做好这份工作。

Using Tomcat (7) this works: 使用Tomcat(7)这可行:

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: 您还需要在构建时包含dbcp库:

<!-- 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在运行时从连接对象获取JDBC驱动程序类名? - How to get JDBC Driver class name from connection object during runtime? 在Java中测试JDBC连接时如何检查有效的驱动程序名称 - how to check Valid driver name while testing JDBC connection in java mongodb的JDBC驱动程序类名是什么? - what is the JDBC driver class name for mongodb? 关于MySQL驱动程序如何使用CLASSPATH? “找不到具有类名com.mysql.jdbc.Driver的驱动程序” - How to use CLASSPATH regarding to mysql drivers? “Could not find driver with class name: com.mysql.jdbc.Driver” OpenJPA-ArgumentException:JDBC驱动程序或数据源类名称 - OpenJPA - ArgumentException: JDBC driver or data source class name OpenJPA:必须在ConnectionDriverName属性中指定JDBC驱动程序或DataSource类名 - OpenJPA: A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property 什么是SQL Server JDBC的驱动程序类名称 - What's the driver class name for SQL Server JDBC 从外部 JAR 加载没有名称的 JDBC 驱动程序 - Load JDBC Driver without name from external JAR 如何使用 HIVE JDBC 驱动程序在列名中使用特殊字符? - How to use special character in column name using HIVE JDBC driver? 如何使用 JDBC 驱动程序连接到带有重音名称的 SQL Server? - How to connect to SQL Server with accent in name using JDBC driver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM