如果无法在jsp中删除记录,则显示错误消息 [英] Show error msg if record is failed to be deleted in jsp

查看:58
本文介绍了如果无法在jsp中删除记录,则显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发基于Web的应用程序,如果用户认为不需要维护,则需要删除父项. 如果父条目在子表中没有任何记录,则该条目将被删除,但如果它在子表中具有记录,那么我想显示该表中存在条目的错误消息.

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.

我正在遵循使用jsp和servlet的MVC架构.

I am following MVC architecture using jsp and servlet.

推荐答案

请注意以下内容,如果您的表定义良好,我的意思是,子记录必须引用父记录(有关FOREIGN KEY的信息).然后,如果您尝试删除父记录,则有很多选择:

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-您可以允许使用默认模式:您必须先删除子级,然后才能删除父级(我认为这是您在说明中所要的).

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-您可以在删除父级时向mysql说"删除所有子级记录.

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-没有子记录,因此一切都按您的期望进行.

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

2-您有引用您要删除的父记录的子记录.在这种情况下,mysql不会删除父记录,并且会返回错误代码1217(无法删除或更新父行:外键约束失败).然后,您可以检测到错误.并假装执行此操作,在这种情况下,向客户端显示错误消息.为了显示消息,这是我写此答案之前发布的评论的副本:

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:

一旦检测到,有两个选项可以将错误发送到jsp 页.您可以使用用户会话发送,也可以使用 重定向到jsp页面,您可以在url中发送一个属性,例如 www.my-app./jsppage?error=1.然后,jsp页面获取该属性,并 显示正确的错误消息.

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.

这篇关于如果无法在jsp中删除记录,则显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆