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.
You can process the error code from the SQLException object.
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.