File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes How to catch sql exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "How to catch sql exception" Watch "How to catch sql exception" New topic

How to catch sql exception

Sameer Nandgaonkar

Joined: May 13, 2007
Posts: 8
In my project i am entering compnay name in emp table.but i want to check the uniqueness of record like the company name should not repeat.For that i am making company name field is that when i insert a record through form it will throw sql exception error.
The problem is i want to catch that error in catch block.and disply message that the entered record is already exist.
I do not know how to do this?can you please help?
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

There are two approaches to this problem. The first is the one you described. You will need to try it once to get the database specific SQL error code. The second is to do a select query first to see if the row already exists and then do the insert.

Note that it is good practice to do the JDBC in a Java class, not from the JSP. It is hard to read/maintain/troubleshoot SQL in a JSP. Adding more complex like a catch block makes this even worse.

What do you have so far with respect to the try/catch block?

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: How to catch sql exception
It's not a secret anymore!