简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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