我需要根据用户选择"is"还是“否”来删除 JTable 中的行。我现在遇到的问题是我收到一条错误消息“检查”附近的正确语法。
if(searchCombo_Job.getSelectedItem().toString().equals("Question ID")){
String search = searchTxt_Job.getText();
int reply = JOptionPane.showConfirmDialog(null, "This will delete all rows selected. Proceed?", "Confirm Delete", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
dlt = "DELETE from job where question_id = " + search + "' ";
pst = conn.prepareStatement(dlt);
//pst.setString(1, search);
((DefaultTableModel)jobCategoryTable.getModel()).removeRow(jobCategoryTable.getSelectedRow());
pst.execute();
}
else {
fillTableJobCategory();
}
最佳答案
如果 job.question_id 是字符串字段,则使用
dlt = "DELETE from job where question_id = '" + search.trim() + "' ";
我会使用查询参数来避免此错误。那么该行将是
dlt = "DELETE from job where question_id = ?";
参数也是 SQL 注入(inject)攻击的预防措施。
关于java - 如何根据组合选择删除JTable中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37640423/