0

如何将返回的计数值存储到变量中,然后可以使用它设置属性?到目前为止,这是我的方法:

public List<Sighting> total() {

     return jdbc.query("select pest_name, count(pest_name) from sighting group by pest_name", new RowMapper<Sighting>() {
         public Sighting mapRow(ResultSet rs, int rowNum) throws SQLException {
                Sighting sighting = new Sighting();
                sighting.setCount(????); // I need to pass a variable to setCount()
                sighting.setPest_name(rs.getString("pest_name"));
                return sighting;
            }      
       });
}

查询中的计数值..

4

3 回答 3

3

您可以指定计数的名称,例如

return jdbc.query("select pest_name, count(pest_name) as pest_count from sighting group by pest_name", new RowMapper<Sighting>() {
         public Sighting mapRow(ResultSet rs, int rowNum) throws SQLException {
                Sighting sighting = new Sighting();
                sighting.setCount(rs.getInt("pest_count"));
                sighting.setPest_name(rs.getString("pest_name"));
                return sighting;
            }      
       });

...或者只是按列号获取它:

return jdbc.query("select pest_name, count(pest_name) from sighting group by pest_name", new RowMapper<Sighting>() {
         public Sighting mapRow(ResultSet rs, int rowNum) throws SQLException {
                Sighting sighting = new Sighting();
                sighting.setCount(rs.getInt(2));
                sighting.setPest_name(rs.getString("pest_name"));
                return sighting;
            }      
       });

您可能需要使用getLong而不是getInt.

于 2013-10-24T13:34:27.567 回答
2

尝试:

Select pest_name, count(pest_name)  as totalCount

并在结果集中尝试

long count = rs.getLong("totalCount")
于 2013-10-24T13:33:50.597 回答
0

说出你的计数。改变

"select pest_name, count(pest_name) from sighting group by pest_name"

"select pest_name, count(pest_name) as pest_count from sighting group by pest_name"
于 2013-10-24T13:33:38.937 回答