Delete JTable and Database Values in Java Swing
Delete JTable and Database Values in Java Swing:
________________________________________________
1.Code for Delete Button
DefaultTableModel model = (DefaultTableModel) table.getModel(); if(table.getSelectedRowCount() == 1){ int response = JOptionPane.showConfirmDialog(null, "Do you want delete selected row?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); if(response == JOptionPane.YES_OPTION){ int row = table.getSelectedRow(); String cell = table.getModel().getValueAt(row, 0).toString(); String sql = "DELETE FROM userrtable WHERE id="+cell; try{ pst = con.prepareStatement(sql); pst.execute(); JOptionPane.showMessageDialog(null, "Deleteted!"); }catch(Exception e){ JOptionPane.showMessageDialog(null, e); } model.removeRow(table.getSelectedRow()); }else if(response == JOptionPane.NO_OPTION){ }else if(response == JOptionPane.CANCEL_OPTION){ } } txtID.setText(""); txtFName.setText(""); txtLName.setText("");
_____________________________________________
Dark Hers
YouTube
https://instagram.com/darkhers
https://www.facebook.com/darkhers
TikTok
https://www.tiktok.com/@darkhers
https://www.pinterest.co.uk/darkhers
Comments
Post a Comment