This content has been marked as final. Show 4 replies
shivasha wrote:Java doesn't have Unique Constraint Exception. That is database exception
While throwing Unique Contraint Exception, getMessage() throws entire statck trace like "insert into <table name> values(<column names>) schemaname.constraintname constarint violation exception
I need to get schemaname.constraintname constarint violation exception alone.
How to extract the message?
Maybe you should try getCause() instead of getMsg()
Edited by: mycoffee on Feb 1, 2013 10:11 AM
Though parsing the reply will work, be warned: Parsing error messages is fragile, and almost always a bad idea for a long term solution.
If you just need a quick debugging solution, fine. But don't rely on the approach for very long. Error messages have a nasty habit of changing over time & software versions. Parsing that works today may break next week, next month, or next year because the message has changed.
Here's an idea I was toying with a while ago:
Get all the constraint names, their expression, and description (if any) from the database's metaData when the application starts up, then copy it to a database table for easy lookup.
When an exception is thrown, send the stack trace to the log file along with the sql statement that caused it and the primary key values used in the sql.
Then if the stack trace has a constraint violation in it, the developer can quickly look up that constraint in the database table to help him quickly understand what went wrong.
If you want to, I suppose your parser can look for those constraint names that are in the database table to parse through the stack trace so the developer doesn't have to manually look it up. Note the constraint names should be named uniquely enough that they will not appear in the stack trace other words.
My question is, when an SQL exception is thrown, how often is it a constraint violation? If not too often, it may not be worth the time to add such a new feature.