使用单个预准备语句删除多个记录

时间:2013-09-25 07:15:14

标签: java mysql prepared-statement

我正在执行两个准备好的语句来删除两个不同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();

我在此请帮忙

1 个答案:

答案 0 :(得分:4)

“IN”条件的语法是:

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

在你的例子中:

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