【问题标题】:sonar shows Method may fail to clean up stream or resource on checked exception声纳显示方法可能无法在检查异常时清理流或资源
【发布时间】:2015-11-19 06:26:23
【问题描述】:
public boolean isAuditTrailEnabled(String masterAccessId) 
{
    String selectQuery = "select t.audit_flag from PARAMETER_MENU_ATTRIBUTES t where t.access_id = '"+masterAccessId+"'";
    PreparedStatement preparedStmnt = null;
    ResultSet resultSet = null;
    String flag = "N";

        if(connection == null)
        {
            initConnection();
        }   
        try {
            **preparedStmnt = connection.prepareStatement(selectQuery);**
            resultSet = preparedStmnt.executeQuery();
            if(resultSet.next())
            {
                flag = resultSet.getString(1);
            }
        }
        catch (SQLException e) {

            try {
                if (resultSet != null){
                    resultSet.close();
                }
                if (preparedStmnt != null){
                    preparedStmnt.close();
                }

            } catch (SQLException e1) {

            }

            return true;
        }
        finally{
            try {
                if (resultSet != null){
                    resultSet.close();
                }
                if (preparedStmnt != null){
                    preparedStmnt.close();
                }
                cleanup();
            } catch (SQLException e1) {
                return false;
            }

        }
        if(flag.equals("N")){
            return false;
        }else{
            return true;
        }

}

它显示了该特定语句的问题。你能帮帮我吗?

【问题讨论】:

    标签: java


    【解决方案1】:

    结果集和语句总是应该在单独的 try catchs 中关闭...甚至 2 if 不能以这种方式编写...或者它应该在第一个 if... 中循环使用...或者保持 else if.. ..先了解基础知识

    if(something)
       {
          if(something)
        }
    or
    
    if((something) &&(something))
    

    【讨论】:

      猜你喜欢
      • 2014-07-20
      • 2017-08-22
      • 2012-09-04
      • 1970-01-01
      • 2018-12-21
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      相关资源
      最近更新 更多