How to determine the DBMS from the Connection object

I have support for supporting multiple RDBMSs. The user has to define the data source as a prerequisite and at the code level I need to define which RDBMS the user will connect to and provide certain RDBMS attributes.

For example:

com.mysql.jdbc.Driver           jdbc:mysql://hostname/ databaseName
oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@hostname:port Number:databaseName

      

As shown above, we can get the connection url or driver name and define the DBMS. But I want to clarify what is the best way to determine which RDBMS user is using. Any help would be greatly appreciated.

+3


source to share


1 answer


It's really simple. See DatabaseMetaData

DatabaseMetaData databaseMetaData = connection.getMetaData();
String databaseName = databaseMetaData.getDatabaseProductName();
String userName = databaseMetaData.getUserName();

      

UPDATE . Please answer @ dnWick's comment .



Yes, support DatabaseMetaData

for a wide range RDBMS

. Through the interface, DatabaseMetaData

we can get metadata about the database associated with us. For example, we can see which tables are defined in the database and which columns each table has. Even we can check the supported functions for the database we connected to.

Example We can see if the database supports multiple transactions, supports UNION or not, etc.

+3


source







All Articles