简体   繁体   English

如何循环结果集并将其保存在地图列表中

[英]How to loop a result-set and Save it in list of maps

I am trying to loop a Result set and store the values in Map and save the map into list but when i execute, for the first loop i'am getting the data correctly but for the second time i'am receiving twice the value and if the loop run for nth time the result i am getting n times. 我正在尝试循环结果集并将值存储在Map中并将地图保存到列表中,但是当我执行时,对于第一次循环,我正确地获取了数据,但是对于第二次循环,我接收了两倍的值,如果循环运行了n次,我得到了n次结果。

Below is the code! 下面是代码!

List<HashMap<String, String>> value = new ArrayList<HashMap<String, String>>();
PreparedStatement selectStmt;
selectStmt = conn.prepareStatement("select * from ETR_INT_partc_DOCM  where DOCM_TYP_CDE ='22'");
ResultSet result = selectStmt.executeQuery();
while (result.next()) {
    HashMap<String, String> resultValues = new HashMap<String, String>();
    resultValues.put("PARTC_ID", result.getString("PARTC_ID"));
    resultValues.put("FILE_NME", result.getString("FILE_NME"));
    resultValues.put("LOC_ID", result.getString("LOC_ID"));
    resultValues.put("CRTE_DTE", result.getString("CRTE_DTE"));
    resultValues.put("CRTE_BY", result.getString("CRTE_BY"));
    value.add(resultValues);
    System.out.println(value);
    resultValues.clear();
}
genarateXML(value, sheet)

Try this: 尝试这个:

ResultSet result = selectStmt.executeQuery();
HashMap<String, String> resultValues;
while (result.next()) {
    resultValues = new HashMap<String, String>();
    resultValues.put("PARTC_ID", result.getString("PARTC_ID"));
    resultValues.put("FILE_NME", result.getString("FILE_NME"));
    resultValues.put("LOC_ID", result.getString("LOC_ID"));
    resultValues.put("CRTE_DTE", result.getString("CRTE_DTE"));
    resultValues.put("CRTE_BY", result.getString("CRTE_BY"));
    value.add(resultValues);
    System.out.println(value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM