31447

Show error msg if record is failed to be deleted in jsp

Question:

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.

Answer1:

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: <a href="https://stackoverflow.com/questions/6720050/foreign-key-constraints-when-to-use-on-update-and-on-delete" rel="nofollow">Foreign key constraints: When to use ON UPDATE and ON DELETE</a>

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:

<blockquote>

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.

</blockquote>

Recommend

  • Type mismatch: cannot convert from Connection to Connection
  • Conditional render in tagfile depending on whether the attribute is specified or not
  • google-maps-sdk-ios - arm 7/iphone 5 issues for Google Maps 1.4.3 for iOS
  • oracle row contention causing deadlock errors in high throughtput JMS application
  • Spring MVC redirect with custom http headers
  • Extjs, handling success or failure when doing a standard submit in a form
  • How to bind comma separated list of values to List
  • Base Internationalization and “Could not find a storyboard named […]”
  • UITableView takes much longer to load when numberOfRows returns a large number
  • Is it mandatory to have a doGet or doPost method?
  • Consuming a web service with the Netbeans Platform
  • Many to Many in Linq using Dapper
  • Extending the Django 1.11 User Model
  • How to Model Ternary Relationship in CakePhp?
  • Are there any side effects from calling SQLAlchemy flush() within code?
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Mixing WebForms and MVC: What should I do with the MasterPage?
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • Struts 2 TextField Tag with an attribute and no value
  • Azure table store snapshot/backup capability
  • Bigquery event streaming and table creation
  • GAE: Way to get reference to an HttpSession from its ID?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • What is the purpose of TaskExecutor in spring?
  • Assign variable to the value in HTML
  • zope_i18n_compile_mo_files doesn't work on a Zeo configuration
  • Webgrid not refreshing after delete MVC
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • one Local Olampyad Questions on Informatic in 2011
  • MVC3 Razor - ListBox pre-select not working
  • Linq Objects Group By & Sum
  • Javascript + PHP Encryption with pidCrypt
  • Large data - storage and query
  • Apache 2.4 - remove | delete | uninstall
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • KeystoneJS: Relationships in Admin UI not updating
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • Net Present Value in Excel for Grouped Recurring CF