Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 541
Luke Avatar asked Apr 19 '11 16:04

Luke


1 Answers

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

like image 124
a_horse_with_no_name Avatar answered Sep 21 '22 07:09

a_horse_with_no_name