You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ResultSet无法存入HashMap问题排查求助

Hey Barbara, let's break down what's going wrong here and fix it step by step.

First: Why your ResultSet appears empty in toHashMap

The most likely culprit is that your ResultSet cursor has already been exhausted before you pass it to the conversion function:

  • In your getDBData method, there's a commented-out debug loop:
    /*while(rset.next()){ System.out.println(rset.getString("ACTUALX")); }*/
    
    If you ran this loop even once (even if you commented it out afterward), the ResultSet cursor moves to the end of the data. By default, ResultSets are forward-only—you can't move back to the start once you've passed the last row.
  • Double-check that you aren't closing the Connection or Statement before calling toHashMap either—ResultSet depends on its parent Statement/Connection being open to access data.

Second: Your toHashMap has a critical design flaw

Storing the entire ResultSet object in your List is a big mistake:

  • ResultSet is a live cursor, not a snapshot of data. Every time you call rset.next(), you're moving the cursor to the next row. Storing the same ResultSet instance multiple times means all List entries will point to the current row of the cursor, not the row you were on when you added it.
  • Once you close the Statement/Connection later, all those ResultSet references will become invalid and throw exceptions.

Revised toHashMap Function

Instead of storing ResultSet, extract each row's data into a separate Map<String, Object> (or a custom POJO if you prefer) and store those in the List. This preserves the actual data instead of a live cursor:

public HashMap<String, List<Map<String, Object>>> toHashMap(ResultSet rset) throws SQLException {
    HashMap<String, List<Map<String, Object>>> hmap = new HashMap<>();
    ResultSetMetaData metaData = rset.getMetaData();
    int columnCount = metaData.getColumnCount();

    while (rset.next()) {
        // Create a map to hold the current row's data
        Map<String, Object> rowData = new HashMap<>();
        for (int i = 1; i <= columnCount; i++) {
            String columnName = metaData.getColumnName(i);
            rowData.put(columnName, rset.getObject(i));
        }

        String processDay = rset.getString("PROCESSDAY");
        // Add the row to the correct group (create list if it doesn't exist)
        hmap.computeIfAbsent(processDay, k -> new ArrayList<>()).add(rowData);
        
        System.out.println(hmap.size());
        // convertToKML(distance, ax, ay, sx, sy, out);
    }
    return hmap;
}

Fix for getDBData (If You Need Debug Prints)

If you want to keep testing the ResultSet before returning it, create a scrollable ResultSet so you can reset the cursor:

// Change how you create the Statement to allow scrolling
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

// After your debug loop, reset the cursor to the start
while(rset.next()){ System.out.println(rset.getString("ACTUALX")); }
rset.beforeFirst(); // Move cursor back to before the first row

Bonus: Resource Management

Don't forget to clean up database resources to avoid leaks. Use try-with-resources blocks in your calling code to auto-close Statements and ResultSets:

try (Statement stmt = conn.createStatement();
     ResultSet rset = stmt.executeQuery(sql_query)) {
    HashMap<String, List<Map<String, Object>>> result = toHashMap(rset);
    // Use your result here
} catch (SQLException e) {
    e.printStackTrace();
}

内容的提问来源于stack exchange,提问作者Barbara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:27:05