My Swing applications throws few exceptions. I tried to catch Integrity Constraint Violation Exception and display message "Duplicate ID". But when that happened, without catching it here: catch(MySQLIntegrityConstraintViolationException ex) it goes to catch (SQLException ex). What I want to do is, catch Integrity Violation exception and display user friendly message instead of technical message comes from ex.getMessage(). How do I do this?
ShippmentTransfer shipTrns = new ShippmentTransfer(shipID, GIN, issueDate, ToStore, itemName, Qty, Driver, Vehicle);
int res = ShipmentTansController.addShipGIN(shipTrns);
if (res > 0) {
JOptionPane.showMessageDialog(null, "Record Added");
resetAll();
}
} catch (DataIntegrityViolationException ex) {
JOptionPane.showMessageDialog(null, "Duplicate ID");
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.gets);
}
}
In order to catch a specific SQLException, you need to compare against SQL state using getSQLState()
method. Ex: SQL State 23 for Data Integrity violation.
catch (SQLException ex) {
if (ex.getSQLState().startsWith("23")) {
JOptionPane.showMessageDialog(null, "Duplicate");
}
}