I have a jtable on my GUI. (tableRealProperty). I need to add, delete and update the data inside the table. and this data are from a database where it has a primary key called ClientID. the columns inside the jtable are “Location” and “Area”.
If i select a certain row from the jtable, it should be deleted as well as from the database, of course. My problem is that, when i click my delete button, not only the selected row is deleted but also all the data inside the table of that certain client..
here’s my code for delete:
private void cmdDelRPropActionPerformed(java.awt.event.ActionEvent evt) {
String sql = "delete FROM tblrealProperty where tblrealproperty.ClientID = ?";
try {
pst = conn.prepareStatement(sql);
pst.setString(1, txtClientID.getText());
((DefaultTableModel)tableRealProperty.getModel()).removeRow(tableRealProperty.getSelectedRow());
pst.execute();
JOptionPane.showMessageDialog(null, "Deleted");
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, e);
}
}
This sounds like you have a foreign key to tblrealproperty set on the client table with cascade delete. That means, if a client is deleted from tblrealproperty, all corresponding data with a foreign key pointed to the deleted row in tblrealproperty are deleted as well. It is a feture inside DB to prevent inconstistencies.