Hi all. I created some exceptions to validate inserts in Oracle because I want to show a friendly message for the user case some error (like PK, FK) occurs. I want to know if it's the best way. Look at the code:
In method insertCustomer I get the SQlException.errorCode() as the Oracle error code and trhow the corresponding exception like PKException, FKException... Is it a good idea? Is there another best way?
Dadonas<br /> <br />Don't gain the world and lose your soul.
Hi Jeanne, when I said "System.out.println("invalid primary key");", I just mentioned an example. Certainly this message will be like this: "System.out.println("This item can't be inserted because it's already registered");". I want to know if these validations that I mentioned are the best way. There is some package of Oracle or else that do it?
Jeanne's question is still a good one. Would the user do anything different if they knew this? If they are the sort of user who understands how to resolve a FK violation for example, and has access to do this, they can probably survive with the SQLExceptions as is, since they will be no different from the messages Oracle returns normally (SQLExceptions just wrap the output from the database).
If you have to do this, I'd avoid explicitly including Oracle specific classes - instead perhaps regex the SQLException message and watch for known Oracle error codes? This way your code wont break if you change databases.
Paul, Jeanne, you're right. The end user shouldn't know what is going on during the insert. But during the transaction, if it throw a SQLException, I need to know what caused this exception to do something (it can be show a friendly message for example). SQLException is generic, it can be caused by a PK, FK, UK... The unique way that I found was getting SQLException.getErrorCode(). For example, the code 1 (for the Oracle) is Primary Key Violation... In DAO classes I throw an especified exception depending the getErrorCode(). Like this:
The interface will call this method and implement the PKException:
Did you understand me? Now I want to know: Is it the best way? There are something newer than this solution?