【问题标题】:SonarQube keeps complaining about Use try-with-resources or close this "PreparedStatement" in a "finally" clauseSonarQube 不断抱怨 Use try-with-resources 或在“finally”子句中关闭此“PreparedStatement”
【发布时间】:2021-12-09 09:34:40
【问题描述】:

我正在尝试从基于用户 ID 的表中获取电子邮件。但是,SonarQube 一直在抱怨我已经拥有的 Use try-with-resources or close this "PreparedStatement" in a "finally" clause。 我已经查看了here 和其他地方的答案,但这些解决方案对我不起作用。 代码如下:

public String getUserEmail(String id) throws SQLException {
        String emailAddress = null;
        String sql = "select email from my_table where id=?";
        PreparedStatement preparedStatement = this.connection.prepareStatement(sql);
        preparedStatement.setString(1, id);
        try {
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                emailAddress = rs.getString("email");
            }
        } catch(SQLException e) {
            throw new TuringClientException("Failed to getUserEmail. ", e);
        } finally {
            preparedStatement.close();
        }
        return emailAddress;
    }

我也尝试将 PreparedStatement preparedStatement = this.connection.prepareStatement(sql) 包装在另一个 try catch 或嵌套 try-catch 中,但这些尝试都不起作用。

【问题讨论】:

    标签: java sql sonarqube prepared-statement


    【解决方案1】:

    setString 可能会抛出 SQLException。如果语句准备成功,然后setString 抛出异常,它将在try 块的外部 这样做,并且语句将永远不会关闭。将prepareStatement 移动到嵌套的try 应该没问题,只要你在右边的finally 块中有clsoe(并且没有看到你做了什么很难说为什么SonarQube 抱怨它),但老实说,使用 try-with-resource 语法会更整洁。注意,顺便提一下,ResultSet 也应该关闭:

    public String getUserEmail(String id) throws SQLException {
        String emailAddress = null;
        String sql = "select email from my_table where id=?";
        try (PreparedStatement preparedStatement = this.connection.prepareStatement(sql) {
            preparedStatement.setString(1, id);
            try (ResultSet rs = preparedStatement.executeQuery()) {
                while (rs.next()) {
                    emailAddress = rs.getString("email");
                }
            } catch(SQLException e) {
                throw new TuringClientException("Failed to getUserEmail. ", e);
            }
        }
        return emailAddress;
    }
    

    【讨论】:

      【解决方案2】:

      你可以试试(用资源试试)

      public String getUserEmail(String id) throws SQLException {
              String emailAddress = null;
              String sql = "select email from my_table where id=?";
              try(PreparedStatement preparedStatement = this.connection.prepareStatement(sql)) {
                  preparedStatement.setString(1, id);
                  ResultSet rs = preparedStatement.executeQuery();
                  while (rs.next()) {
                      emailAddress = rs.getString("email");
                  }
              } catch(SQLException e) {
                  throw new TuringClientException("Failed to getUserEmail. ", e);
              }
              return emailAddress;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-31
        • 1970-01-01
        • 2021-10-08
        • 1970-01-01
        • 2021-02-14
        • 1970-01-01
        • 2021-12-14
        • 2020-05-13
        相关资源
        最近更新 更多