Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the best way to get database name use java metadata

In my application using different databases (postgres, mssql, oracle …). What is the best way to get database name use metadata. I was trying to use this approach:

try (final Connection connection = jdbcTemplate.getDataSource().getConnection()) {
    final DatabaseMetaData metaData = connection.getMetaData();
    final String databaseName = StringUtils.substringAfterLast(connection.getMetaData().getURL(), "/");

It works in this case:

spring.datasource.url=jdbc:mysql://localhost:3306/2022_2

But in this case it doesn’t work:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

spring.datasource.url=jdbc:sqlserver://localhost:1433;DatabaseName=test_mssql;encrypt=true;trustServerCertificate=true;

>Solution :

You may consider using Connection#getCatalog() for your case.

Please see the following example below:

try (final Connection connection = jdbcTemplate.getDataSource().getConnection()) {
    String databaseName = connection.getCatalog();
} catch (SQLException e) {
    e.printStackTrace();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading