[英]What is the best way to get database name use java metadata
In my application using different databases (postgres, mssql, oracle...).在我的应用程序中使用不同的数据库(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:但在这种情况下它不起作用:
spring.datasource.url=jdbc:sqlserver://localhost:1433;DatabaseName=test_mssql;encrypt=true;trustServerCertificate=true;
You may consider using Connection#getCatalog() for your case.您可以考虑为您的案例使用Connection#getCatalog() 。
Please see the following example below:请看下面的例子:
try (final Connection connection = jdbcTemplate.getDataSource().getConnection()) {
String databaseName = connection.getCatalog();
} catch (SQLException e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.