[英]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.