Java 获取连接的mysql数据库名称(JDBC)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26441303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:30:10  来源:igfitidea点击:

Get the connected mysql database name (JDBC)

javamysqljdbc

提问by Paullo

How can get the name of the database name from connection object

如何从连接对象中获取数据库名称的名称

try {
    this.ds = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/amger");
} catch (NamingException ne) {
}
Connection conObj = ds.getConnection();

How do I get that Database name from con

我如何从 con 中获取该数据库名称

采纳答案by Gord Thompson

Probably the most straightforward way to get the database name from the JDBC Connection object itself is via the getCatalog()method:

从 JDBC Connection 对象本身获取数据库名称的最直接方法可能是通过以下getCatalog()方法:

Connection#getCatalog()

连接#getCatalog()

However, as Konstantin pointed out in his comment below, that value will not change if the current MySQL database is changed by issuing a USE dbnamestatement.

但是,正如 Konstantin 在下面的评论中指出的那样,如果通过发出USE dbname语句更改当前 MySQL 数据库,则该值不会更改。

getCatalog()might still be useful in an application that

getCatalog()在应用程序中可能仍然有用

  • does not change databases, or
  • does things "The JDBC Way" by using setCatalog()to change the current database,
  • 不更改数据库,或
  • 通过使用setCatalog()更改当前数据库来执行“JDBC 方式” ,

but for MySQL, using SELECT DATABASE()appears to be safer overall.

但对于 MySQL 来说,使用SELECT DATABASE()似乎总体上更安全。

Note also that this potential discrepancy between getCatalog()and the actual current database depends on the behaviour of the particular JDBC driver. Out of curiosity I tried something similar with the Microsoft JDBC Driver 4.0 for SQL Server and .getCatalog()was indeed aware of the change to the current database immediately after running a USE dbnamestatement. That is, the code

另请注意,getCatalog()与实际当前数据库之间的这种潜在差异取决于特定 JDBC 驱动程序的行为。出于好奇,我尝试了与 Microsoft JDBC Driver 4.0 for SQL Server 类似的东西,并且.getCatalog()确实在运行USE dbname语句后立即意识到对当前数据库的更改。也就是说,代码

String connectionUrl = "jdbc:sqlserver://localhost:52865;"
        + "databaseName=myDb;" + "integratedSecurity=true";
try (Connection con = DriverManager.getConnection(connectionUrl)) {
    System.out.println(String.format(
            "getCatalog() returns: %s", 
            con.getCatalog()));
    try (Statement s = con.createStatement()) {
        System.out.println("           Executing: USE master");
        s.execute("USE master");
    }
    System.out.println(String.format(
            "getCatalog() returns: %s", 
            con.getCatalog()));
} catch (Exception e) {
    e.printStackTrace(System.out);
}

produced the following results:

产生了以下结果:

getCatalog() returns: myDb
           Executing: USE master
getCatalog() returns: master

回答by Konstantin V. Salikhov

If you know that DB is Mysql you could just perform SELECT DATABASE()on your connection and read the resulset with current database name in it.

如果您知道 DB 是 Mysql,您可以SELECT DATABASE()在您的连接上执行并读取包含当前数据库名称的结果集。

Here is descriptionof DATABASE function.

这里是对 DATABASE 函数的描述

回答by SMA

Let's assume you used url as "jdbc:mysql://localhost/test"

假设您使用 url 作为“jdbc:mysql://localhost/test”

Then do the following:

然后执行以下操作:

DatabaseMetaData dmd = connection.getMetaData();
String url = dmd.getURL();
System.out.println(url.substring(url.lastIndexOf("/") + 1));