日期:2014-05-17  浏览次数:20791 次

请问如何在后台取得当前jdbc连接driver名
比如我跳转到后台,可以通过jdk获取当前连接的数据库的驱动名称么,如果jdk不行,用ssh框架可以实现么,越简单越好。

------解决方案--------------------
Connection#getMetaData

DatabaseMetaData#getDriverName


只要能获取到Connection,就可以取出来
------解决方案--------------------
同意一楼

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class Main {

public static void main(String[] args) throws Exception {
String url = "jdbc:odbc:databaseName";
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String user = "guest";
String password = "guest";

try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);

// Get the MetaData
DatabaseMetaData metaData = conn.getMetaData();

// Get driver information
System.out.println("Driver Informaion");
System.out.println(metaData.getDriverName());
System.out.println(metaData.getDriverVersion());
// Get schema information
System.out.println("Schemas");
ResultSet schemas = metaData.getSchemas();
while (schemas.next()) {
System.out.println(schemas.getString(1));
}
// Get table information
System.out.println("Tables");
ResultSet tables = metaData.getTables("", "", "", null);
while (tables.next()) {
System.out.println(tables.getString(3));
}
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
------解决方案--------------------
探讨

引用:

Connection#getMetaData

DatabaseMetaData#getDriverName


只要能获取到Connection,就可以取出来

我的数据库连接是通过hibernate.cfg.xml文件指定的