我试图从jtable删除已删除的行,但由于它是一个主键(客户端ID),我不能只更新它的表,我必须更新它是外键的表(动物和约会(rv)),但我如何才能做到这一点,我试图在删除后从DB重新填充表,但没有工作
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");
}
}
您可以手动删除选定的行:
[...]
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);
} [...]