2

Further to

I can either

  1. duplicate each of my SQL table constraints in my Java code (e.g. explicitly run an SQL statement to check if a row being added has a primary key that already exists). Or

  2. I can try to catch SQLExceptions in a common class/method that converts the exception into a message that makes sense to an end user.

The first seems like a lot of work. It also seems like unnecessary duplication. I still need the second approach as well in case some constraint gets overlooked or added without corresponding Java code. Or because there can be other types of SQLException (e.g. if the Ethernet lead falls out)

The trouble with the latter is that the exceptions are hard to parse. SQLException.getCause().getMessage() produces something like

The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'CURRENCY_PK' defined on 'CURRENCY'.

Obviously I'd prefer to have a message more like

Sorry, that currency code has already been defined, please edit the existing currency instead or use a different currency code for the currency you are adding.

I imagine the .getMessage() text is likely to depend on the run-time environment and user language and other things. I don't see a way to get, for example, a numeric code that I could use to lookup my own user-friendly message.


Q: Is there a way to produce user-friendly messages in Java from SQLExceptions?

Community
  • 1
  • 1
RedGrittyBrick
  • 3,827
  • 1
  • 30
  • 51
  • Re: getting a numeric code: What is wrong with [getErrorCode](http://docs.oracle.com/javase/8/docs/api/java/sql/SQLException.html#getErrorCode--) and [getSQLState](http://docs.oracle.com/javase/8/docs/api/java/sql/SQLException.html#getSQLState--)? Note however this is a hard problem. SQLExceptions are relatively low-level and this can't always be translated one-on-one to a user friendly message, and it differs per database, etc. – Mark Rotteveel Aug 08 '14 at 14:05
  • 1
    @Mark: I didn't find `getErrorCode` -- it didn't appear in the autocomplete suggestions in Eclipse because my error-handling class was working with Throwables in general (Doh!). Obviously I should engage my brain and also read the JavaDocs carefully rather than relying on Eclipse :(. Thanks for the tip :). – RedGrittyBrick Aug 08 '14 at 14:11
  • What I once did was caught the exception converted to string and setting it to response I sent it to UI while in ui i caught the response and display like if it is exception some error occured or else the update message. – SparkOn Aug 08 '14 at 14:13
  • @RedGrittyBrick No problem, also look at the type of `SQLException`, this sometimes (but not always) conveys additional information. However in the worst case you will need to parse (or pattern match) the database specific exception messages – Mark Rotteveel Aug 08 '14 at 14:34
  • Also look up `SQLIntegrityConstraintViolationException`. – Joe Aug 08 '14 at 17:09

0 Answers0