1

I am developing web based application where I needs to delete parent entries if user thinks it will no need to maintain. If parent entry doesn't have any record in child table then entry will get deleted but if it has records in child table then I wants to show error message with entry exists in which table.

I am following MVC architecture using jsp and servlet.

Amit Shirke
  • 81
  • 1
  • 5
  • 15
  • 1
    So, your question is how to detect that error in the servlet and show it on the jsp? (just confirm, so that I can give you a proper answer) – Luis Alves Jan 16 '15 at 16:14
  • Yes. I am calling delete method in my java class from servlet and wants to show error message on the jsp page – Amit Shirke Jan 17 '15 at 05:02
  • Ok, then you can detect the delete problem on your servlet. Once detected, there are two options, to send the error to the jsp page. You can send it using the user session, or, when you are redirecting to the jsp page, you can send an attribute in the url like www.my-app./jsppage?error=1. Then the jsp page gets the attribute and display the proper error message. – Luis Alves Jan 17 '15 at 12:12
  • thank you for your answer and this seems easy but can you please tell how can I detect problem in my servlet? – Amit Shirke Jan 19 '15 at 05:37

1 Answers1

0

Notice the following, if your tables are well defined I mean, the child records must reference the parent records (read about FOREIGN KEY). Then, if you try to delete the parent record you have many options:

1- You can allow the default mode: You can't delete the parent without delete the child first (I think this is what you want from the description).

2- You can "say" to mysql to delete all the child records when you delete the parents.

For more information about this, read this post: Foreign key constraints: When to use ON UPDATE and ON DELETE

Assuming you want to follow the first option, when you want to delete the parent key two things may happen.

1- There's no child record, and therefor everything goes how you expect.

2- You have child records referencing the parent record you want to delete. In this case, mysql won't delete the parent record, and will return you a error with code 1217 (Cannot delete or update a parent row: a foreign key constraint fails). Then you can detect the error. And do what you pretend to do with it, in this case to present a error message to the client. To show the message, here's a copy of the comment I post before I wrote this answer:

Once detected, there are two options, to send the error to the jsp page. You can send it using the user session, or, when you are redirecting to the jsp page, you can send an attribute in the url like www.my-app./jsppage?error=1. Then the jsp page gets the attribute and display the proper error message.

Community
  • 1
  • 1
Luis Alves
  • 1,286
  • 12
  • 32
  • thank you but my question is how to detect that error in servlet because exception is thrown from java class itself. You said Once detected and gave me options but actually I don't know how to detect this in Servlet – Amit Shirke Jan 21 '15 at 06:44
  • You need to catch the SQLException and get the error code – Luis Alves Jan 21 '15 at 10:53