aspose file tools*
The moose likes Servlets and the fly likes Problems while inserting record Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problems while inserting record" Watch "Problems while inserting record" New topic
Author

Problems while inserting record

John Vergis
Ranch Hand

Joined: Feb 24, 2004
Posts: 59
Hi
I am trying to add an employee record. The problem I am facing is, on clicking on "Create" I get an exception that the unique constraint is violated on the Emp_No field,
While I debugged, I found that it called the doPost method and based on the action parameter set ( in this case addNewEmp) it calls the appropriate method in the servlet)
and that the record is created, but strangely it again tries to insert the same record, that is the method doAddNewEmp(..) is called again.... I have no idea as why this is happening, this has taken a few days of mine without any progress..

Could ne1 help me out in figuring why this is happening pls....??

Thanks
John
:::JSP CODE SNIPPET:::



:::SERVLET CODE SNIPPET:::



In my case it always is redirected to the page in the catch (SQLException) part.. indicating SQL exception has occurred.. But the record has been inserted into the database...

Any suggestions please?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17256
    
    6

John, my first and biggest suggestion is to move all your JDBC code out of the Servlet. A Servlet should be used only to handle the request and response, any logic should be placed in a Plain old Java object. Then you can test you POJO to make sure the insert works, and not have to worry about the Servlet part. By having your JDBC code in your Servlet, you are heavily coupling Web stuff with JDBC.

I don't see any problem elsewhere though.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Jyothi Lature
Ranch Hand

Joined: Oct 31, 2001
Posts: 60
Hi John,

The button update in your servlet page is already of SUBMIT type, which means action will be executed automatically when you click on the upadte button. You dont have to explicitly call theForm.submit(); again. This will cause the form to submit it second time. So, comment the line -- [B]theForm.submit();{/B]. This should correct your code.


Jyothi<br />Sun Certified Java Programmer<br />Brainbench Certified for Javascript
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
or you can change your button type to "button", instead of submit. incase you want to set some values in your hidden fields afterwards on button click. it will save you. but for now i think either way is fine.
John Vergis
Ranch Hand

Joined: Feb 24, 2004
Posts: 59
Thanks All for your response. Thanks Jyoti for pointing that out, it was the form.submit() in the javascript function that was creating havoc.. i deleted that line and now things are working fine..

Thanks All for your time and response

John
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems while inserting record