jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes Exception management Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Exception management" Watch "Exception management" New topic
Author

Exception management

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi all
I have a database with unique constraint. How can I differentiate the "unique constraint exception" (and manage this one in the application) and an another exception that i don't want to manage (just catch and no information to final user).
Do I have to check the constraint with a request before and throw my own exception or is it possible to recognize the exception.???
thx
Gayathri Prasad
Ranch Hand

Joined: Jun 25, 2003
Posts: 116
Hi,
When ever there is unique contraint violation the Database throws some number exception. We need to identify the exceptions depending on the number the DBengine returns. I think we dont have any exception classes for this and hence we need to code in our custom exception class.
Cheers,
Gaya3
Tim Morrow
Greenhorn

Joined: Sep 17, 2003
Posts: 11
As mentioned previously there is a vendor-specific error code available from SQLException. You can check that value and handle it appropriately. You'd need to look up the particular error for your database and determine its error code. Take a look at the Javadoc for java.sql.SQLException.
Also, the original error may be nested a few exceptions deep, so you may also have to traverse the nested exceptions inside SQLException.
For example, assuming your "Unique constraint violated" error code is error code "1" for your database (believe this is correct for Oracle) you might write:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception management
 
Similar Threads
How can we know exact position of exception
exception related to data constraint
EntityExistsException
Getting 0 rows returned when selecting with null values
cmp UNIQUE KEY CONSTRATINT