我想收集以下12个指标(TotalEmail,SentEmail等)。为此我使用的是本地方法(getValues())。在那里我使用了12个Statement和ResultSet对象。它工作正常。你能否建议我减少变量的内存使用量。
getValues(){
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;
}
答案 0 :(得分: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;
}
希望它有所帮助!