有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从数据库到hashmap的java值给出了键和值的重复序列

我试图从数据库中检索数据并将其放入hashmaps中,然后将hashmaps添加到名为listOfLocations的位置对象列表中的位置对象
我的输入是一个包含128个具有属性名称的位置对象的列表,我使用该列表查找在数据库中这些位置找到的物种
我从数据库中检索了该位置的名称和硅藻种类
当我浏览rs.next中的数据时,我检查物种是否在我的程序(数组)的初始输入中,并根据该值给hashmap键一个值present或not
但问题是,我最终发现所有的hashmap都是一样的。它们都包含相同的键和值。 我已经检查了rs.getString(1)和rs.getString(2)中的内容,这与我在HashMap中观察到的重复序列不同
我已经盯着我的代码看了几个小时,但我似乎不明白为什么会发生这种情况。我希望有人能在逻辑上指出我的错误

try (Connection connection = DbConnection.getConnection()) {
    PreparedStatement preparedStatement;
    String query = "SELECT l.name, d.species " +
            "FROM Entry e " +
            "INNER JOIN Location l ON l.name = e.name " +
            "INNER JOIN Diatom d ON d.taxonKey = e.taxonKey " +
            "WHERE d.species != '' AND l.name IN ";
    assert connection != null;
    StringBuilder whereIn = new StringBuilder("(?");
    //make as many placeholders as there are locations in the input.
    for (int i = 0; i < listOfLocations.size() - 1; ++i) {
        whereIn.append(", ?");
    }
    //ORDER BY location.name so I can go through all diatoms per location.
    whereIn.append(") ORDER BY l.name");
    query += whereIn.toString();
    preparedStatement = connection.prepareStatement(query);
    for (int i = 1; i <= listOfLocations.size(); ++i) {
        preparedStatement.setString(i, listOfLocations.get(i - 1).getName());
    }
    //currentLocation will keep track of which location is currently being iterated.
    String currentLocation = "";
    int counter = 0;
    ResultSet rs = preparedStatement.executeQuery();
    HashMap<String, String> diatomHashmap = new HashMap<>();
    while (rs.next()) {
        //if currentLocation is not set yet (only the first location), set currentLocation.
        if (currentLocation.equals("")) {
            currentLocation = rs.getString(1);
        }
        //if statement, if I'm still on the currentLocation, add the species to the diatomHashmap.
        //Present or notPresent based on input (diatomArray).
        if (currentLocation.equals(rs.getString(1))) {
            if (diatomArray.contains(rs.getString(2))) {
                diatomHashmap.put(rs.getString(2), "Present");
            } else {
                diatomHashmap.put(rs.getString(2), "notPresent");
            }
        } else {
            //else will be called when currentLocation does NOT equal the location in rs.getString(1).
            //add the made diatomHashmap to Location object in listOfLocations (list of Location objects).
            listOfLocations.get(counter).setDiatoms(diatomHashmap);
            //set currentLocation to the new location being iterated.
            currentLocation = rs.getString(1);
            //increase counter so we move to the next Location object in listOfLocation.
            counter++;
            //clear hashmap to start adding new species to the current location.
            diatomHashmap.clear();
            //makes the first new entry in the hashmap.
            if (diatomArray.contains(rs.getString(2))) {
                diatomHashmap.put(rs.getString(2), "Present");
            } else {
                diatomHashmap.put(rs.getString(2), "notPresent");
            }
        }
    }
    //one last setDiatoms because the last location will never reach the else statement.
    listOfLocations.get(counter).setDiatoms(diatomHashmap);
} catch (SQLException ex) {
    System.out.println(ex);
}

共 (1) 个答案

  1. # 1 楼答案

    HashMap<String, String> diatomHashmap = new HashMap<>();
    

    看起来你只定义了一个HashMap。所有setDiatoms设置相同的对象

    listOfLocations.get(counter).setDiatoms(diatomHashmap);`