This tutorial shows how you can delete specific data from your MySQL database using your Java Application with Netbeans IDE. Before anything else be sure that you already perform a save and update operation in your java program.
This feature is the same from your update feature, to update or delete a specific row from your database we need to set a unique id or key from your database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try{ | |
if (data1.getText().equals("")){ | |
JOptionPane.showMessageDialog(null, "Data1 is empty"); | |
}else if (data2.getText().equals("")){ | |
JOptionPane.showMessageDialog(null, "Data2 is empty"); | |
}else if (data3.getText().equals("")){ | |
JOptionPane.showMessageDialog(null, "Data3 is empty"); | |
}else{ | |
int p = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this item?","Delete",JOptionPane.YES_NO_OPTION); | |
if (p==0){ | |
String str="DELETE FROM tablename WHERE id='" + id.getText() + "'"; | |
pst=conn.prepareStatement(str); | |
pst.execute(); | |
JOptionPane.showMessageDialog(null,"Data Deleted!"); | |
clear(); | |
loadbn(); | |
} | |
} | |
} | |
catch (Exception e){ | |
JOptionPane.showMessageDialog(this, e.getMessage()); | |
} |
The code above uses a Try-catch exception, this block of codes can catch all the errors trows from your statement.
We also use an if statement, this block of codes notify the user if some fields are empty.
Related Articles:
Save data in MySQL using Java
Update data in MySQL using Java
0 Comments