I can update data properly using Java Swing and MySQL. If id
is present in the database JOptionPane
shows a dialog box that Updated data Succesfully and even id
is not present in the database JOtionPane
shows the same dialog box. How to solve that?
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventoryms", "root", "");
Statement stmt = conn.createStatement();
String query = "update inventory set name = '"+txtName.getText()+"', price = '"+txtPrice.getText()+"', category = '"+comboCat.getSelectedItem().toString()+"' where id = '"+txtId.getText()+"' ";
stmt.executeUpdate(query);
JOptionPane.showMessageDialog(null,"Data updated successfully!!");
conn.close();
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, "Error is: " + ex);
}
}
Show the same dialog box that updated data successfully.