简体   繁体   English

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

[英]Values from database into hashmap gives repeating sequence of key and values

I'm trying to retrieve data from my database and put it in hashmaps and add the hashmaps to location objects in a list of location objects named listOfLocations.我正在尝试从我的数据库中检索数据并将其放入哈希映射并将哈希映射添加到名为 listOfLocations 的位置对象列表中的位置对象。
My input is a list of 128 location objects with property name, which I use to find the species found at those locations in the database.我的输入是带有属性名称的 128 个位置对象的列表,我用它来查找在数据库中这些位置找到的物种。
From the database I retrieve the location name and the diatom species of that location.我从数据库中检索位置名称和该位置的硅藻种类。
When I go through the data in rs.next, I check if the species was in the initial input of my program (diatomArray) and give the hashmap key a value of present or not based on that.当我查看 rs.next 中的数据时,我会检查该物种是否在我的程序的初始输入 (diatomArray) 中,并根据该值为 hashmap 键指定是否存在的值。
But the problem is, I end up with all diatomHashmaps being the same.但问题是,我最终得到的所有 diatomHashmaps 都是相同的。 They all contain the same keys and values.它们都包含相同的键和值。 I have checked what is in rs.getString(1) and rs.getString(2) and that isn't the same repeating sequence as I'm observing in the diatomHashmap.我已经检查了 rs.getString(1) 和 rs.getString(2) 中的内容,这与我在 diatomHashmap 中观察到的重复序列不同。
I have been staring at my code for hours, but I can't seem to figure out why this is happening.我一直盯着我的代码几个小时,但我似乎无法弄清楚为什么会发生这种情况。 I hope someone can point out my fault in the logics.我希望有人能指出我逻辑上的错误。

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);
}
HashMap<String, String> diatomHashmap = new HashMap<>();

Looks you defined only one diatomHashmap.看起来你只定义了一个 diatomHashmap。 all setDiatoms sets the same object.所有setDiatoms设置相同的对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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