Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems while inserting record

 
John Vergis
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jyothi Lature
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic