Delete Data using MySQL Database and Java with Netbeans IDE

0
67

This tutorial entitled Delete Data using MySQL Database and Java with Netbeans IDE will help you on how to create a Delete features using your Java and Netbeans.

This is the continuation of my last topic entitled How to Load Data from MySQL Database to Table Element in Java. This program can delete data from a selected row of data from Java table. Follow all the steps to complete this tutorial.

Delete Data using MySQL Database and Java with Netbeans IDE

  1. Add a new button inside your load data form and rename the button text to “Delete”. We use that button for our delete function.

2. Double click the new created button and insert the following codes below.

  1. try{
  2. int row=jTable1.getSelectedRow();
  3. String id = (jTable1.getModel().getValueAt(row, 0).toString());
  4. String nna = (jTable1.getModel().getValueAt(row, 1).toString()) + " " + (jTable1.getModel().getValueAt(row, 2).toString());
  5.  
  6. int p = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete " + nna + "?","Delete",JOptionPane.YES_NO_OPTION);
  7. if (p==0){
  8. String str="DELETE FROM userinfo WHERE idUserInfo='" + id + "'";
  9. pst=conn.prepareStatement(str);
  10. pst.execute();
  11. JOptionPane.showMessageDialog(null,"Sucessfully Deleted!");
  12. }
  13. }catch(Exception e){
  14. JOptionPane.showMessageDialog(rootPane, e);
  15. }

3. Run your program and the output should look like the image below. When the user click the delete button, a confirmation box appear asking the user to delete the item or not. If the user select yes, the data selected from the table will be deleted.

Related Articles You May Like:

 

Facebook Comments
(Visited 74 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here