码迷,mamicode.com
首页 > 编程语言 > 详细

零基础学习java------35---------删除一个商品案例,删除多个商品

时间:2019-10-07 23:29:10      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:date   多选框   cts   学习java   迭代   override   案例   获取数据   extends   

一. 删除一个商品案例

将要操作的表格

技术图片

 

 

思路图

技术图片

 

 前端代码

技术图片
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
    <button>
        <a href="/day13/GetAllProducts">查詢商品列表</a>
    </button>
    <br />
    <br />
    <br /> 

    <table border="1px" cellspacing="0" width="100%">
        <tr>
            <th>id</th>
            <th>name</th>
            <th>category</th>
            <th>pnum</th>
            <th>description</th>
            <th>描述</th>
        </tr>
        <!-- 迭代获取数据,即遍历 -->
        <c:forEach items="${p_list }" var="product">
            <tr>
                <td>${product.id}</td>
                <td>${product.name}</td>
                <td>${product.price}</td>
                <td>${product.category}</td>
                <td>${product.pnum}</td>
                <td>${product.description}</td>
                <td>
                    <a href="/day13/DeleteOneProduct?id=${product.id }">删除</a>
                </td>
        </c:forEach>
    </table>

</body>
</html>
View Code

此处删除直接使用a标签发送请求,用form表单也可以,但写起来麻烦:

技术图片

 

 

后段部分代码

servlet

DeleteOneProduct

技术图片
public class DeleteOneProduct extends HttpServlet {
    private static final long serialVersionUID = 1L;
    DeleteOneService deleteOneService = new DeleteOneServiceImpl();
   
    // 删除一个商品
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        try {
            deleteOneService.deleteOneProduct(id);
            response.sendRedirect("/day13/GetAllProducts");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
View Code

service层

DeleteOneService接口

技术图片
public interface DeleteOneService {
    /**
     * 删除一个商品
     */
    public void deleteOneProduct(String id) throws Exception;
}
View Code

接口实现类(DeleteOneServiceImpl)

技术图片
public class DeleteOneServiceImpl implements DeleteOneService{
    DeleteOneDao deleteOneDao = new DeleteOneDaoImpl();
    @Override
    public void deleteOneProduct(String id) throws Exception {
        deleteOneDao.deleteOneProductFromDB(id);
    }
}
View Code

dao层

DeleteOneDao接口

技术图片
public interface DeleteOneDao {
    /**
     * 从数据库删除一个商品
     */
    public void deleteOneProductFromDB(String id) throws Exception;
}
View Code

接口实现类(DeleteOneDaoImpl)

技术图片
public class DeleteOneDaoImpl implements DeleteOneDao {
    // 创建数据库连接池,并放在静态代码块中
    static QueryRunner runner;
    static {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        runner = new QueryRunner(dataSource);
    }
    /**
     * 从数据库中删除数据
     */
    @Override
    public void deleteOneProductFromDB(String id) throws SQLException {
        String sql = "delete from products where id= ?";
        runner.update(sql, id);
    }
}
View Code

 

二. 删除多个商品案例

思路:删除一个商品是前端发送删除请求(携带一个商品的id),删除多个商品则是前端发送删除请求(携带多个商品的id)

    前端该如何选中多个商品呢?----->多选框,此处只能使用form表单,不然不能将多个id传至后端

前端代码

技术图片
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
    <button>
        <a href="/day13/GetAllProducts">查詢商品列表</a>
    </button>
    <br />
    <br />
    <br /> 
    <form action="/day13/DeleteManyProducts" method="POST">
    <input type="submit" value="删除多个商品" />
    <table border="1px" cellspacing="0" width="100%">
        <tr>
            <th>id</th>
            <th>name</th>
            <th>category</th>
            <th>pnum</th>
            <th>description</th>
            <th>描述</th>
        </tr>
        <!-- 迭代获取数据,即遍历 -->
        <c:forEach items="${p_list }" var="product">
            <tr>
                <td><input type="checkbox" name="id" value="${product.id}" /></td>
                <td>${product.name}</td>
                <td>${product.price}</td>
                <td>${product.category}</td>
                <td>${product.pnum}</td>
                <td>${product.description}</td>
                <td>
                    <a href="/day13/DeleteOneProduct?id=${product.id }">删除</a>
                    
                </td>
        </c:forEach>
    </table>
    </form>
</body>
</html>
View Code

部分截图

技术图片

 

 

后段部分

DeleteManyProduct

技术图片
public class DeleteManyProducts extends HttpServlet {
    private static final long serialVersionUID = 1L;
    ProductsService productsService = new ProductsServiceImpl();   
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收参数
        String[] idList = request.getParameterValues("id");
        for (String id : idList) {
            try {
                productsService.deleteManyProducts(id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        response.sendRedirect("/day13/GetAllProducts");
    }
}
View Code

其他部分类似删除第一个商品的案例

 

三  编辑(修改商品信息)

技术图片

 

 修改商品信息

(1)修改页面:添加一个修改商品的编辑页面

(2)点击编辑按钮,将当前的id发送到后台,后台根据此id查询商品

(3)将要修改的商品展示到点击编辑后跳转的页面上

(4)修改,将修改完的数据提交到后台,后台完成修改

(5)跳转到页面展示

 

零基础学习java------35---------删除一个商品案例,删除多个商品

标签:date   多选框   cts   学习java   迭代   override   案例   获取数据   extends   

原文地址:https://www.cnblogs.com/jj1106/p/11632581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!