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

时间:2013-12-27 17:33:58

标签: java list collections map hashmap

我正在尝试循环结果集并将值存储在Map中并将地图保存到列表中但是当我执行时,对于第一个循环我正确地获取数据但是第二次我接收到两次如果循环运行第n次,结果我得到n次。

以下是代码!

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)

1 个答案:

答案 0 :(得分:0)

试试这个:

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);
}