开发者

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

开发者 https://www.devze.com 2023-02-26 19:13 出处:网络
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

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 SQ开发者_开发问答LException. 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号