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

  • 本文关键字:键和值 数据库 hashmap java mysql
  • 更新时间 :
  • 英文 :


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

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<>();

看来你只定义了一个diatomHashmap。所有CCD_ 1设置相同的对象。

listOfLocations.get(counter(.setDiatoms(diaatoHashmap(;`

最新更新