Reputation: 5554
I am using JDBC, and am running a query. Is there a way to get the reason it failed from the SQLException object returned?
In particular, I want to know if my query violated a foreign key constraint (and which one), or a key constraint.
Would this result be vendor-specific? Just in case, I am using the postgresql-8.4-701.jdbc4.jar driver. If it is vendor-specific, where would I find the codes?
EDIT: I want to do this dynamically - i.e.
if(violated foreign key constraint on attribute x) {
return 5;
} else if (violated primary key constraint) {
return 7;
} else {
return 0;
}
Or something like that.
EDIT: According to this post, there are no vendor-specific error codes for PostgreSQL JDBC. Dunno if that's still valid.
Upvotes: 5
Views: 4834
Reputation: 4334
You can process the error code from the SQLException object.
sqlException.getErrorCode()
This retrieves the vendor-specific exception code for this SQLException object. By using the vendor-specific error code, you can branch to the required block of code.
Upvotes: 6
Reputation: 1380
try printing the stack trace (e is of SQLException type)
e.printStackTrace()
or getting the Message
e.getMessage()
These may give you some explanation of why the query failed
Upvotes: 0