我需要将MySQL数据库的结果集值添加到Hash类型的映射中
映射。能否请您提出如何将所有值添加到哈希映射中的建议。

 public Map<String,paramObj> fetchThresholdValues( request){
    Connection conn = null;
    PreparedStatement pstmt = null;
    BaseDAO db = new BaseDAOImpl();
    paramObj obj = null;
    Map<String, paramObj> oilAndMetalValueMap = new HashMap<String, paramObj>();
    String query = "";
    try {

        conn = db.getConnection();
        query = QueryConstants.GET_PARAMETER_THRESHOLD;
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, request.getPopulationId());
        ResultSet rs = pstmt.executeQuery();

        while (rs.next()) {
            obj = new paramObj();
            obj.setParameterName(rs.getString("PARAMETER_NAME"));
            obj.setWatchLowLow(rs.getFloat("WATCH_LOW_LOWER_THRESHOLD"));
            obj.setWatchLowHigh(rs.getFloat("WATCH_LOW_HIGHER_THRESHOLD"));
            obj.setWatchHighLow(rs.getFloat("WATCH_HIGH_LOWER_THRESHOLD"));
            obj.setWatchHighHigh(rs.getFloat("WATCH_HIGH_HIGHER_THRESHOLD"));
            obj.setCautionLowLow(rs.getFloat("CAUTION_LOW_LOWER_THRESHOLD"));
            obj.setCautionLowHigh(rs.getFloat("CAUTION_LOW_HIGHER_THRESHOLD"));
            obj.setCautionHighLow(rs.getFloat("CAUTION_HIGH_LOWER_THRESHOLD"));
            obj.setCautionHighHigh(rs.getFloat("CAUTION_HIGH_HIGHER_THRESHOLD"));
            obj.setWarningLowLow(rs.getFloat("WARNING_LOW_LOWER_THRESHOLD"));

            oilAndMetalValueMap.put(rs.getString("PARAMETER_NAME"), threshold);
        }

最佳答案

从文档here


为了获得最大的可移植性,每行中的结果集列应为
按从左到右的顺序读取,并且每一列应只读一次。


因此,使用rs.getString只能读取一列一次。在PARAMETER_NAME中设置obj时,请在放入地图时使用。

oilAndMetalValueMap.put(obj.getParameterName(), obj)

关于java - 将结果集值添加到Hashmap Map <String,Object>类型中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62278235/

10-13 05:03