Search code examples
javapostgresqlexceptionjdbcerror-handling

Postgres JDBC: Especific error code of PSQLException?


When writing java code that uses an Oracle database, one can always catch SQLException an read an specific Oracle error with e.getErrorCode(). For example, error 28001 means expired password, 28000 is blocked account, 1017 is wrong user/passsword, etc.

That way I can manage different errors the appropiate way.

But with PostgreSQL databases e.getErrorCode() always returns 0, even when catching Postgres-specific PSQLException.

The Question

  • Is there a way that I don't know of to get an specific error code for a Postgres database exception in Java other than trying to parse the error message (which by the way could be in any localized language)?

Solution

  • Have you tried looking at getSqlState() instead? See also: http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html