aspose file tools*
The moose likes JDBC 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
Bookmark "How to catch sql exception" Watch "How to catch sql exception" New topic
Author

How to catch sql exception

Sameer Nandgaonkar
Greenhorn

Joined: May 13, 2007
Posts: 8
Hi,
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 unique.so 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
Marshal

Joined: May 26, 2003
Posts: 30770
    
156

Sameer,
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?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to catch sql exception