简体   繁体   English

使用 java 元数据获取数据库名称的最佳方法是什么

[英]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.

相关问题 在 Spring Data JPA 中,有没有最简单的方法来获取表元数据(列名列表)信息? 我可以在通用数据库上使用哪个 - Is there any Simplest way to get Table metadata (column name list) information, in Spring Data JPA ? which could I use on universal database 在Java中使用序列化的最佳方法是什么? - What is the best way to use Serialization in Java? java swing使用日期的最佳方法是什么 - what is the best way to use dates with java swing 在 Java 中生成唯一且简短的文件名的最佳方法是什么 - What is the best way to generate a unique and short file name in Java Java中轮询程序的数据库连接的最佳方法是什么 - What is the best way to take database connection for pollling programs in java 使用Java从数据库读取UDT的最佳方法是什么? - What's the best way to read a UDT from a database with Java? 将 xml 导入数据库的最佳方法是什么(使用 java) - What is the best way to import xml to a database (using java) 将关系数据库表关联到Java类的最佳方法是什么? - What is the best way of relating relational database tables into Java classes? 在Java中获取数据库元数据的最简单方法? - Easiest way to obtain database metadata in Java? 在File的Java IO中使用Delim的最佳方法是什么 - What's best way to use delim in Java IO from File
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM