焉知愚之乐
saojipo.com

jsp批量删除数据库数据

刚学习到jsp的增删改查操作,老师布置了作业:全选表单进行批量删除操作,记录一下第一次用自己的思路实现代码,虽然现在没接触到框架,但我想,框架操作也应该是这样一个过程

思路:

1:在jsp页面中获取到要删除对象的pid,一个或多个,并提交数据到servlet
2:servlet中用request.getParameter()方法接收pid,并传递到service层中进行业务处理
3:在service层中将接收的pid进行处理(分割文本,拼接文本,设置查询参数)
3:在dao层进行数据库的操作

1:全选/全不选操作及处理get提交参数

<script type="text/javascript"
    src="${pageContext.request.contextPath }/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
    function batchDelProduct() {
        var msg = "您确定要删除选中的商品吗?";   
        if (confirm(msg)==true){   
            var allcheckbox = "";
            var becheckbox = "";
            $("input[name=checkOne]").each(function(){ //遍历table里的全部checkbox
                allcheckbox += $(this).val() + ","; //获取所有checkbox的值
                if($(this).prop("checked")) //如果被选中
                    becheckbox += $(this).val() + ","; //获取被选中的值
            });
            if(becheckbox.length > 0) //如果获取到
                becheckbox = becheckbox.substring(0, becheckbox.length - 1); //把最后一个逗号去掉
                window.location = "${pageContext.request.contextPath}/admin/delProduct?pid="+becheckbox;
        }else{   
        return false;   
        }   
}</script>

2:表单部分

 <table cellspacing="0" cellpadding="1" rules="all"
                            bordercolor="gray" border="1" id="DataGrid1"
                            style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; BORDER-LEFT: gray 1px solid; WIDTH: 100%; WORD-BREAK: break-all; BORDER-BOTTOM: gray 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #f5fafe; WORD-WRAP: break-word">
                            <tr
                                style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; HEIGHT: 25px; BACKGROUND-COLOR: #afd1f3">
                                <td align="center" width="5%">全选/全不选<input type="checkbox"
                                    onclick="checkAll()" id="checkAll" /></td>
                                <td align="center" width="18%">序号</td>
                                <td align="center" width="17%">商品图片</td>
                                <td align="center" width="17%">商品名称</td>
                                <td align="center" width="17%">商品价格</td>
                                <td align="center" width="17%">是否热门</td>
                                <td width="7%" align="center">编辑</td>
                                <td width="7%" align="center">删除</td>
                            </tr>
                            <c:forEach items="${productList }" var="pro" varStatus="vs">
                                <tr onmouseover="this.style.backgroundColor = 'white'"
                                    onmouseout="this.style.backgroundColor = '#F5FAFE';">
                                    <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                        width="5%"><input type="checkbox" name="checkOne" value="${pro.pid }"></td>
                                    <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                        width="18%">${vs.count }</td>
                                    <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                        width="17%"><img width="40" height="45"
                                        src="${pageContext.request.contextPath }/${pro.pimage}"></td>
                                    <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                        width="17%">${pro.pname }</td>
                                    <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                        width="17%">${pro.shop_price }</td>
                                    <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                        width="17%">${pro.is_hot==1?"是":"否" }</td>
                                    <td align="center" style="HEIGHT: 22px"><a
                                        href="${ pageContext.request.contextPath }/admin/updateProductUI?pid=${pro.pid }">
                                            <img
                                            src="${pageContext.request.contextPath}/images/i_edit.gif"
                                            border="0" style="CURSOR: hand">
                                    </a></td>

                                    <td align="center" style="HEIGHT: 22px"><a
                                        href="javascript:void(0);" onclick="delProduct('${pro.pid}')">
                                            <img
                                            src="${pageContext.request.contextPath}/images/i_del.gif"
                                            width="16" height="16" border="0" style="CURSOR: hand">
                                    </a></td>
                                </tr>
                            </c:forEach>
                        </table>

3:servelt接收参数并传递参数到业务层

         // 获得pid
        String pid = request.getParameter("pid");
        // 将pid传递给service层
        AdminProductService service = new AdminProductService();
        try {
            service.delProductByPid(pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // 重定向到列表
        response.sendRedirect(request.getContextPath()+"/admin/productList");

4:业务层处理pid(将pid分割成单个,用数组接收,并拼接dao层需要的sql语句和QueryRunner的提交参数)

	public void delProductByPid(String pid) throws SQLException {
		AdminProductDao dao = new AdminProductDao();
		// 分割pid文本,用数组接收
		String[] pids = pid.split(",");
		// 新建object数组,用作QueryRunner操作参数,长度为分割后的pid数组长度
		Object[] param = new Object[pids.length];
		String sql = "";
		for (int i = 0; i < pids.length; i++) {
			// 如果是第一个参数
			if (i == 0) {
				sql = "pid=? ";
			} else {
				sql += "or pid=? ";
			}
			// 为param数组赋值
			param[i] = pids[i];
		}
		// 拼接sql语句,sql.substring(0, sql.length()-1)代表删除原sql最后一个空格
		sql = "DELETE FROM product WHERE " + sql.substring(0, sql.length()-1);
		// 传递参数到dao层进行数据库操作
		dao.delProductByPid(sql, param);
	}

5:dao层进行数据库操作

	public void delProductByPid(String sql, Object[] param) throws SQLException {
		QueryRunner runner = new QueryRunner(MyDataSource.getDataSource());
		runner.update(sql,param);
	}
赞(0)

评论 抢沙发

评论前必须登录!