0

我想收集以下 12 个指标(TotalEmail、SentEmail 等)。为此,我使用了本地方法(getValues())。因为我使用了 12 个 Statement 和 ResultSet 对象。它工作正常。你能建议我任何其他方法来减少变量的内存使用吗?

获取值(){

private Statement TotalEmail    =   null;
private Statement SentEmail     =   null;
private Statement FailEmail     =   null;   
private Statement ActiveUser    =   null;
private Statement FailLogin     =   null;
private Statement SentLogin     =   null;   
private Statement TotalBilling  =   null;
private Statement FailBilling   =   null;
private Statement SuccessBilling=   null;
private Statement TotalSubmission   =   null;
private Statement FailSubmission    =   null;
private Statement SuccessSubmission =   null;

private ResultSet TotalEmailRS  =   null;
private ResultSet SentEmailRS   =   null;
private ResultSet FailEmailRS   =   null;
private ResultSet ActiveUserRS  =   null;
private ResultSet FailLoginRS   =   null;
private ResultSet SentLoginRS   =   null;
private ResultSet TotalBillingRS    =   null;
private ResultSet FailBillingRS     =   null;
private ResultSet SuccessBillingRS  =   null;
private ResultSet TotalSubmissionRS =   null;
private ResultSet FailSubmissionRS  =   null;
private ResultSet SuccessSubmissionRS   =   null;

}

4

1 回答 1

0

我建议以下解决方案:

除了为每个查询创建一个特定的 mysql 查询之外,您是否可以调整您的代码以使您只有一个查询功能?

我会这样做:

//As 'parameters' I would write String colMatch, but you adapt it to your needs
private String getTotalEmail( ...parameters... ){ 
    return getDataMysql(column, table, colMatch);
}

private String getDataMysql(String column, String table, String colMatch){
          PreparedStatement query = null;
          ResultSet rs = null;
          String data = "";
          try{
            String queryQ = "SELECT " + column + " FROM " + table +
                    " WHERE " + column + " = '" + colMatch + "' ";
            query = Connection.getConnection().prepareStatement(queryQ);
            rs = query.executeQuery(queryQ);

            //if you know there will be only one possible value found
            while ( rs.next() ){
                data = rs.getString(column);
            }          
        }catch(Exception e){
            System.out.println(e);
            System.out.println("Error query getDataMysql");
        }finally{
            // code for closing connection
        }
        return data;
    }

希望能帮助到你!

于 2013-08-09T12:38:08.287 回答