如何从 Java 中的 HashMap 获取特定值
How can I get a specific value from a HashMap in Java
我正在尝试从 HashMap
中获取值 ID,但我没有成功,这是代码
try {
Connection lig = DriverManager.getConnection(dbURL,"root", "");
Statement inst = lig.createStatement();
String nome = txtNquest.getText();
int id = questionarios3.get(nome);
Connection lig3;
try {
lig3 = DriverManager.getConnection(dbURL, "root", "");
PreparedStatement inst2 = lig3.prepareStatement("SELECT pergunta,id FROM perguntas WHERE Questionarios_id=?");
inst2.setInt(1, id);
ResultSet res = inst2.executeQuery();
while (res.next()) {
perguntasmap2.put(res.getString("pergunta"),
res.getInt("id"));
}
lig3.close();
} catch (SQLException e2) {
JOptionPane.showMessageDialog(frame,"Impossível ligar à base de dados\n"
+ e2.getLocalizedMessage());
}
inst.executeUpdate("DELETE FROM respostas WHERE idPerguntas="+ id2);
我正在尝试使用 HashMap
'perguntasmap2' 的 ID,并将该 ID 用于 DELETE 中的 id2,更具体
perguntasmap2.put(res.getString("pergunta"),res.getInt("id"));
使用此 ID 并输入此值 (id2)
inst.executeUpdate("DELETE FROM respostas WHERE idPerguntas="+ id2);
我想删除那个特定问题的所有答案='respostas'='pergunta',我还没有尝试任何操作,因为我真的不知道如何去做,在我的数据库我得到了更多的值 'perguntas' 和 'respostas' 都用 id 组织起来以帮助
好的,我有一些疑问:
perguntasmap2.put(res.getString("pergunta"),res.getInt("id"));
为什么将id放在与地图中的值相关的字段中?放在Key域不是更好吗?喜欢
perguntasmap2.put(res.getInt("id"), res.getString("pergunta"));
和
inst.executeUpdate("DELETE FROM respostas WHERE idPerguntas="
+ id2);
您尝试使用 idPerguntas= 删除 respostas,但是您将有多个 pergunta id,也许您必须使用 WHERE idPerguntas in (id)
?
如果你想从 pergunta 的所有 id 中删除所有 respostas,并且你将 pergunta 的 id 放在 hashmap 的关键字段中,就像我上面显示的那样,你可以这样做:
String ids = new org.apache.commons.lang.StringUtils().join(map.keySet(), ",");
你不需要 apache commons,你可以用一个简单的 for 循环来做到这一点。
"DELETE FROM respostas WHERE idPerguntas in (" + ids + ")");
Se tiver mais alguma dúvida, estamos ai! :)
我正在尝试从 HashMap
中获取值 ID,但我没有成功,这是代码
try {
Connection lig = DriverManager.getConnection(dbURL,"root", "");
Statement inst = lig.createStatement();
String nome = txtNquest.getText();
int id = questionarios3.get(nome);
Connection lig3;
try {
lig3 = DriverManager.getConnection(dbURL, "root", "");
PreparedStatement inst2 = lig3.prepareStatement("SELECT pergunta,id FROM perguntas WHERE Questionarios_id=?");
inst2.setInt(1, id);
ResultSet res = inst2.executeQuery();
while (res.next()) {
perguntasmap2.put(res.getString("pergunta"),
res.getInt("id"));
}
lig3.close();
} catch (SQLException e2) {
JOptionPane.showMessageDialog(frame,"Impossível ligar à base de dados\n"
+ e2.getLocalizedMessage());
}
inst.executeUpdate("DELETE FROM respostas WHERE idPerguntas="+ id2);
我正在尝试使用 HashMap
'perguntasmap2' 的 ID,并将该 ID 用于 DELETE 中的 id2,更具体
perguntasmap2.put(res.getString("pergunta"),res.getInt("id"));
使用此 ID 并输入此值 (id2)
inst.executeUpdate("DELETE FROM respostas WHERE idPerguntas="+ id2);
我想删除那个特定问题的所有答案='respostas'='pergunta',我还没有尝试任何操作,因为我真的不知道如何去做,在我的数据库我得到了更多的值 'perguntas' 和 'respostas' 都用 id 组织起来以帮助
好的,我有一些疑问:
perguntasmap2.put(res.getString("pergunta"),res.getInt("id"));
为什么将id放在与地图中的值相关的字段中?放在Key域不是更好吗?喜欢
perguntasmap2.put(res.getInt("id"), res.getString("pergunta"));
和
inst.executeUpdate("DELETE FROM respostas WHERE idPerguntas="
+ id2);
您尝试使用 idPerguntas= 删除 respostas,但是您将有多个 pergunta id,也许您必须使用 WHERE idPerguntas in (id)
?
如果你想从 pergunta 的所有 id 中删除所有 respostas,并且你将 pergunta 的 id 放在 hashmap 的关键字段中,就像我上面显示的那样,你可以这样做:
String ids = new org.apache.commons.lang.StringUtils().join(map.keySet(), ",");
你不需要 apache commons,你可以用一个简单的 for 循环来做到这一点。
"DELETE FROM respostas WHERE idPerguntas in (" + ids + ")");
Se tiver mais alguma dúvida, estamos ai! :)