java 获取Sonar构建问题-使用try-with资源或在finally子句中关闭此“连接”[重复]

bvjxkvbb  于 2023-04-19  发布在  Java
关注(0)|答案(1)|浏览(370)

此问题已在此处有答案

How should I use try-with-resources with JDBC?(5个答案)
关闭28分钟前.

public String getDatabaseProductName() throws SQLException {
    Connection connection = dataSource.getConnection();
    DatabaseMetaData metaData = connection.getMetaData();
    return metaData.getDatabaseProductName();
}

我正在尝试解析-Use try-with resources or close this "connection" in a finally clause
要关闭的连接并返回- DatabaseProductName。

gwbalxhn

gwbalxhn1#

public String getDatabaseProductName() throws SQLException {
    try(Connection connection = dataSource.getConnection()) {
        DatabaseMetaData metaData = connection.getMetaData();
        return metaData.getDatabaseProductName();
    }
}

相关问题