Java 如何从 jdbc 连接中获取驱动程序类名(不是驱动程序名)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22317215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get driver class name (not driver name) from jdbc connection
提问by Nigel Thomas
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 我需要得到我的驱动程序类名称。
Everytime I try like
每次我尝试喜欢
DataSource ds = (DataSource)context.lookup("java:/jdbc/myDataSource")
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()
ds.getConnection().getMetatData().getDriverName()
It is returning just Oracle JDBC Driverinstead 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.
如何从上下文中获取类名。
回答by Micha? Niklas
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
回答by nico
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 :
我使用基于反射的“尝试”算法。OracleDataSource 在“驱动程序”属性中包含驱动程序,并且可能有很多 DataSource 执行相同的操作。所以以下:
Field field = dataSource.getClass().getDeclaredField("driver");
field.setAccessible(true);
return field.get(dataSource).getClass().getName();
do the job.
做这份工作。
回答by Jason Pyeron
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(唯一)。
回答by user1767316
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>