简体   繁体   English

DAO类中的CPD错误

[英]CPD errors in a DAO class

I have a DAO class which has multiple methods.In each method I used variable name "result" for ResultSet and "statement" for PreparedStatement and a closeResources() method to close the PreparedStatement and the Connection. 我有一个包含多个方法的DAO类。在每个方法中,我对ResultSet使用变量名“ result” ,对PreparedStatement使用变量名“ statement” ,并使用closeResources()方法关闭PreparedStatement和Connection。 I have used a DataManager class which has createConnection() method,getConnection() method.There are like 10 mwthods in my DAO.I am using DAO factory method to get DAO object in BO.This is Snippet of code,that is shown as violation when I ran CPD tool in eclipse.It is showing this snippet of code as a violation in around 6 to 8 methods of my DAO. 我使用的DataManager类具有createConnection()方法,getConnection()方法。我的DAO中有10个类。我正在使用DAO工厂方法在BO中获取DAO对象。这是代码段,如下所示:当我在Eclipse中运行CPD工具时发生违规,这表明我的DAO的大约6到8种方法都将此代码段显示为违规。

PreparedStatement statement=null;

try{

PreparedStatement statement=connection.prepareStatement(query);

//statements to set data in query

ResultSet result=statement.executeQuery();

if(result.next){

//some operation

   }
}
catch(SQLException e){

//encapsulating sql exception....


throw new BusinessException(e);

}
finally{

closeResources(connection,statement);

}

Since for many methods in DAO use the same approach,CPD is showing the above code as violation and I think no way you can make this piece of code to be more modular.My question is using the same variable name like "result" in many methods is a best practice or not.To clear the violation,I need to rename the "result" to "result1" , "result2" etc but i feel those names as not meaningful. 由于DAO中的许多方法都使用相同的方法,因此CPD将上述代码显示为违规,我认为您无法使这段代码更具模块化。我的问题是在许多情况下使用相同的变量名,例如“结果”方法是否是最佳实践。要清除冲突,我需要将“结果”重命名为“ result1”“ result2”等,但是我觉得这些名称没有意义。

Note:I am at the end of my training.I didn't work on Springs or Struts, I know just Servlets and JSP.I am doing a case study,and it is the first time I am using PMD,CPD tools. 注意:我已经结束训练。我没有在Springs或Struts上工作,我只知道Servlets和JSP。我正在做一个案例研究,这是我第一次使用PMD,CPD工具。

Use Template method pattern: 使用模板方法模式:

public void runQuery(Callback callback) {
    PreparedStatement statement=null;
    try{
        PreparedStatement statement=connection.prepareStatement(query);
        ResultSet result=statement.executeQuery();
        if(result.next){
            callback.onRow(result);  //crucial!
       }
    }
    catch(SQLException e){
        throw new BusinessException(e);
    }
    finally{
        closeResources(connection,statement);
    }
}

interface Callback {
    void onRow(ResultSet resultSet);
}

You use your method like this: 您可以这样使用方法:

runQuery(new Callback() {
    public void onRow(ResultSet result) {
        //extract one row here
    }
});

The only pieces of code that change are various Callback implementations, extracting rows from different tables. 唯一更改的代码是各种Callback实现,它们从不同的表中提取行。 Once you familiarize with this pattern, check out JdbcTemplate which is built around that idea, but is much, much more powerful, yet still low-level and fast. 熟悉此模式后,请查看围绕该想法构建的JdbcTemplate ,但它功能强大得多,但仍然底层且快速。 You don't need full to use it. 您不需要整个可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM