What is the proper way to catch and handle ORA-00001 SQLException with JDBC?

Luke picture Luke · Apr 19, 2011 · Viewed 10.1k times · Source

I'm creating a simple form that stores entered data in an extremely simple Oracle database table via a Java Servlet using JDBC. That table is using the email address as a primary key. If a user submits a form multiple times with the same email address, the execute function fails and throws a SQLException. The exception's string is the following:

java.sql.SQLException: ORA-00001: unique constraint (...removed...) violated

In this scenario, I would like to catch this exception and deal with it by telling the user that the form cannot be submitted multiple times with the same email address. What is the proper way to handle ORA-00001 separately and differently from any of the other SQLExceptions that can be thrown by execute? A string compare could obviously work here, but that seems like a poor solution.

Answer

a_horse_with_no_name picture a_horse_with_no_name · Apr 19, 2011

If you don't need to be DBMS independent use SQLException.getErrorCode()

It returns the vendor specific numeric error code. For ORA-0001 this would be 1