我正在尝试从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来组织帮助
答案 0 :(得分:0)
好的,有些问题我有疑问:
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 maisalgumadúvida,estamos ai! :)