• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to catch sql exception

 
Sameer Nandgaonkar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic