从结果集中获取统计信息

时间:2015-07-30 07:37:33

标签: java collections resultset

我有一个返回5个值的结果集。我在POJO中设置了这些数据,并将pojo添加到列表中。

datapojo = new OutputViewDataPOJO();
datapojo.setTime(resultado.getInt(1));
datapojo.setCommandDesc(resultado.getString(2));
datapojo.setOkError(resultado.getString(3));
datapojo.setResultDesc(resultado.getString(4));
datapojo.setResultCode(resultado.getInt(5));                    
listPojo.add(datapojo);

    [Description:Call connect, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting open complete, TimeStamp:2468, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Reading card, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting read card, TimeStamp:10329, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Reading card, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting read card, TimeStamp:6671, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Reading card, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting read card, TimeStamp:6704, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Reading card, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting read card, TimeStamp:6578, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Reading card, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting read card, TimeStamp:6578, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Reading card, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting read card, TimeStamp:6594, OkError:OK, ResultCode:0, ResultDesc:null.]

顶部列出的数据打印在控制台中,现在我的问题是: 如何获取描述“等待读卡”的TimeStamp总和。我需要做一些统计,在这种情况下获取每个描述的timeStamp的媒体值,我不知道如何做到这一点。 拜托,帮助我!

1 个答案:

答案 0 :(得分:1)

如果要将数据存储在数据库中然后检索,则需要使用这些列创建databasetable(f.e。user

name -age -city -car model -house type

然后您可以存储数据( Insert into user(name, age, city, car_model, house_type) values (?,?,?,?,?) 并检索(Select * from user where city = 'Madrid')等。

您可以在一条评论中提及POJO实体:

public class User
{
   private int id;
   private String name;
   private int age;
   private String city;
   private String carModel;
   private String houseType;

  //getters , setters
}

在数据库中创建相应的表格,并使用InsertSelect查询处理数据。

如果您不想将数据存储在数据库中,可以创建Map以便于过滤。 f.e。

 User user = ...; //an instance of User
 Map<String, List<User>> userCityMap = new HashMap<String,List<User>>(); 
 List<User> userWithThatCity = map.get(user.getCity());
 if(userWithThatCity == null)
 {
   userWithThatCity = new ArrayList<User>();
   userCityMap.put(user.getCity(), userWithThatCity);
 }
 userWithThatCity.add(user);
 ...

我希望这会有所帮助。