Java Mysql Integrity Constraint Violation Exception -


my swing applications throws few exceptions. tried catch integrity constraint violation exception , display message "duplicate id". when happened, without catching here: catch(mysqlintegrityconstraintviolationexception ex) goes catch (sqlexception ex). want is, catch integrity violation exception , display user friendly message instead of technical message comes ex.getmessage(). how 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 catch specific sqlexception, need compare against sql state using getsqlstate() method. ex: sql state 23 data integrity violation.

 catch (sqlexception ex) {                     if (ex.getsqlstate().startswith("23")) {                           joptionpane.showmessagedialog(null, "duplicate");                     }                } 

found here


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -