从DB中删除后更新Jtable

时间:2018-01-17 16:15:12

标签: java swing derby

我试图从jtable中删除已删除的行,但由于它是主键(客户端ID),我不能只更新其表,我必须更新表所在的位置&# 39;一个外键(动物和约会(rv)),但我怎么能实现它我试图在删除后从数据库中重新填充表但没有工作

int f = table.getSelectedRow();
if(f != -1) { 
    int result = JOptionPane.showConfirmDialog(this, "Confirm the Delete", "Warning!!!", JOptionPane.OK_CANCEL_OPTION) ) 
    if (result == JOptionPane.OK_OPTION) {
        String l = table.getModel().getValueAt(f,0).toString();
        int l1 = Integer.parseInt(l);

        try { 
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
            Connection cnx = DriverManager.getConnection("jdbc:derby:C:\\Users\\user pc\\Documents\\NetBeansProjects\\project\\projet","root","root");
            Statement stm= cnx.createStatement(); 
            stm.executeUpdate("delete from rv where cid="+l1+"");
            stm.executeUpdate("delete from animal where cid="+l1+" ");
            stm.executeUpdate("delete from client where id="+l1+"");  
        } catch(SQLException e) {
            e.getStackTrace();
        } catch(ClassNotFoundException ex) {
            Logger.getLogger(all.class.getName()).log(Level.SEVERE, null, ex);
        } catch(InstantiationException ex) {
            Logger.getLogger(all.class.getName()).log(Level.SEVERE, null, ex);
        } catch(IllegalAccessException ex) {
            Logger.getLogger(all.class.getName()).log(Level.SEVERE, null, ex);
        }             
    } else {
        JOptionPane.showMessageDialog(null,"Please select the row you want to delete");
    }
}

1 个答案:

答案 0 :(得分:0)

您可以手动删除所选行:

[...]
try { 
    Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
    Connection cnx = DriverManager.getConnection("jdbc:derby:C:\\Users\\user pc\\Documents\\NetBeansProjects\\project\\projet","root","root");
    Statement stm= cnx.createStatement(); 
    stm.executeUpdate("delete from rv where cid="+l1+"");
    stm.executeUpdate("delete from animal where cid="+l1+" ");
    stm.executeUpdate("delete from client where id="+l1+"");  

    ((DefaultTableModel) table.getModel()).removeRow(f);
} [...]