简体   繁体   中英

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.
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.
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.
But the problem is, I end up with all diatomHashmaps being the same. 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.
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. all setDiatoms sets the same object.

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

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