我正在执行两个准备好的语句,以删除两个具有不同ID的记录。我想在单个准备好的语句执行中删除两条记录
我的代码是这样的

String sqlDelete,sqlSelect2;
  sqlDelete = "delete from vul_detail where scanno=? and id=?";//
            PreparedStatement ps2=conn.prepareStatement(sqlDelete);
//  System.out.println("PS Created Successfully");
  ps2.setString(1, "scan_2");
  ps2.setInt(2, 2);
 // ps2.setInt(3, 5);
  ps2.executeUpdate();
  System.out.println("first record deleted");
  ps2.setString(1, "scan_2");
  ps2.setInt(2, 5);
  ps2.executeUpdate();
  System.out.println("second record deleted");
     sqlSelect2="select * from vul_detail;";
           stmt=conn.createStatement();
System.out.println("select query created");
ResultSet rs2 = stmt.executeQuery(sqlSelect2);


我尝试查询为

sqlDelete = "delete from vul_detail where scanno=? and id=?,?";
PreparedStatement ps2=conn.prepareStatement(sqlDelete);
System.out.println("PS Created Successfully");
 ps2.setString(1, "scan_2");
 ps2.setInt(2, 2);
 ps2.setInt(3, 5);
 ps2.executeUpdate();


我在这方面遇到异常,请帮忙

最佳答案

“ IN”条件的语法为:

expression in (value1, value2, .... value_n);


在您的示例中:

sqlDelete = "delete from vul_detail where scanno = ? and id in (?, ?)";

关于java - 使用单个准备好的语句删除多于1条记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18998540/

10-09 01:29