[英]How to determine the DBMS from Connection object
I have a usecase of supporting multiple RDBMS. 我有一个支持多个RDBMS的用例。 User should define the data source as a prerequisite and at the code level i have to determine which RDBMS the user going to connect with and provide specific RDBMS attributes.
用户应该将数据源定义为先决条件,并且在代码级别,我必须确定用户要连接哪个RDBMS并提供特定的RDBMS属性。
Eg:, 例如:,
com.mysql.jdbc.Driver jdbc:mysql://hostname/ databaseName
oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@hostname:port Number:databaseName
As shown above we can retrieve connection url or may be driver name and identify the RDBMS. 如上所示,我们可以检索连接URL或可能是驱动程序名称并标识RDBMS。 But i want to clarify what is the best way to identify which RDBMS user is using.
但我想澄清一下识别哪个RDBMS用户正在使用的最佳方法。 Any help would be greatly appreciated.
任何帮助将不胜感激。
This is really simple. 这很简单。 See DatabaseMetaData
请参见DatabaseMetaData
DatabaseMetaData databaseMetaData = connection.getMetaData();
String databaseName = databaseMetaData.getDatabaseProductName();
String userName = databaseMetaData.getUserName();
UPDATE To answer @dnWick comment. 更新回答@dnWick评论。
Yes, This DatabaseMetaData
support for the wide range of RDBMS
. 是的,这个
DatabaseMetaData
支持广泛的RDBMS
。 Through the DatabaseMetaData
interface we can obtain metadata about the database that we have connected. 通过
DatabaseMetaData
接口,我们可以获取有关我们已连接的数据库的元数据。 For instance, we can see what tables are defined in the database, and what columns each table has, Even we can check supported features for the database we have connected. 例如,我们可以看到数据库中定义了哪些表,以及每个表有哪些列,甚至我们可以检查我们连接的数据库的支持功能。
Example We can see if a database has support for multiple transactions, supports for UNION or not, etc., 示例我们可以看到数据库是否支持多个事务,是否支持UNION等,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.