简体   繁体   English

如何使用deleteRow()方法从多个表中删除行

[英]How to delete rows from multiple tables using deleteRow() method

I've been working on the following code: 我一直在研究以下代码:

import java.sql.*;


public class deleteRowDemo
{
    public static void main(String args[])
    {
       Connection con;
       Statement stmt;
       ResultSet rs;
       PreparedStatement ps;
       try    
       {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:Project");
            stmt=con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
            rs=stmt.executeQuery("select Mword, from Model,Stud where Mword=Sword");
            while(rs.next())
            {
                 ps=con.prepareStatement("insert into Compare (Cword) values (?)");
                 ps.setString(1,rs.getString(1));
                 ps.executeUpdate();  
                 rs.deleteRow();
            }
            con.close();   
       } catch(Exception e)
       {
          System.out.println(e);  
       }
   }
}

There are two tables Model and Stud. 有两个表Model和Stud。 The two tables are being compared and all the words that are similar in both the tables are inserted in another table called Compare, at the same time i wish to delete the words from both the Stud and Model tables. 正在比较两个表,并且两个表中所有相似的词都插入到另一个表中,称为Compare,同时我希望从Stud和Model表中删除这些词。 The function deleteRow() works fine when the select statement has only one table , but how to work it out for complex select statements with multiple tables. 当select语句只有一个表时,函数deleteRow()可以正常工作,但是对于具有多个表的复杂select语句,如何使用该函数。

You should delete the row with a DELETE-statement. 您应该删除带有DELETE语句的行。 Exactly how this should be done depends upon your data model. 究竟应如何执行取决于您的数据模型。

Instead of rs.deleteRow(); 代替rs.deleteRow(); in the while loop, you could for instance do: 在while循环中,您可以例如执行以下操作:

PreparedStatement deleteModel  = con.prepareStatement("DELETE Model WHERE Mword = ?"); // You might need to modify this query if Mword is not unique in Model.
deleteModel.setString(1, rs.getString(1));
deleteModel.executeUpdate();
//Do the same for Stud.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM