• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Problems while inserting record

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic