Java 如何减少语句和结果集对象??

Java 如何减少语句和结果集对象??,java,scope,Java,Scope,我想收集以下12个指标(TotalEmail、SentEmail等)。为此,我使用了一个本地方法(getValues())。因为我使用的是12个语句和ResultSet对象。它工作得很好。你能给我建议其他减少变量内存使用的方法吗 getValues(){ }我建议以下解决方案: 不是为每个查询创建一个特定的mysql查询,而是可以调整代码,使您只有一个查询函数吗 我会这样做: //As 'parameters' I would write String colMatch, but you ada

我想收集以下12个指标(TotalEmail、SentEmail等)。为此,我使用了一个本地方法(getValues())。因为我使用的是12个语句和ResultSet对象。它工作得很好。你能给我建议其他减少变量内存使用的方法吗

getValues(){


}我建议以下解决方案:

不是为每个查询创建一个特定的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;
    }

希望有帮助

这不是你真正的代码。方法中不能有
private
关键字。这些看起来像实例变量,实例变量不需要显式初始化为
null
,因为它们已经是
null
。哇,非常感谢。我得到了答案。这条路真的很酷。但有一个疑问,它是否会降低任何性能?
//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;
    }