How to get the cause of a SQLException from a SQLException object

I am using JDBC and running a request. Is there a way to get the reason for the failure of the returned SQLException object?

Specifically, I want to know if my query has violated a foreign key constraint (and which one) or a key constraint.

Will this result be vendor specific? Just in case, I'm using the postgresql-8.4-701.jdbc4.jar driver. If it depends on the provider, where can 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 it's still relevant.

+3


source to share


2 answers


You can handle the error code from the SQLException object.

sqlException.getErrorCode()

      



This retrieves the provider-specific exception code for this SQLException object. Using the vendor specific error code, you can navigate to the required block of code.

+4


source


try to print the stack trace (e is of type SQLException)

e.printStackTrace()

      

or get a message



e.getMessage()

      

This may give you some explanation of why the request was refused.

0


source







All Articles