5.11作业---Servlet + JDBC 完成新增商品 和查询商品的功能

1 编写 新增商品 和 查询商品的页面
2 使用Servlet + JDBC 完成新增商品 和查询商品的功能
3 实现新增商品之后 重定向到 查询商品的页面

1.封装 查询 和 新增 方法

public class ProductDao {
    //查询所有商品
    public List<Product> findAllProduct() throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.dataSource);
        String sql = "select * from product";
        List<Product> productList = qr.query(sql, new BeanListHandler<Product>(Product.class));
        return productList;
    }
    //插入新的商品
    public int addProduct(Product product) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.dataSource);
        String sql = "insert into product values(?,?,?,?,?,?)";
        int i = qr.update(sql, product.getPid(), product.getPname(), product.getPrice(), product.getPdesc(), product.getPflag()
                , product.getCid());
        return i;
    }

}

2.主页前端页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
</head>
<body>
<a href="addProduct.html">插入数据</a>
</body>
</html>

 

3.新增数据前端页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>插入数据</title>
</head>
<body>
<a href="index.html">返回主页</a>
   <form action="addProduct" method="post">
       <p>商品ID:<input type="text" name="pid"></p>
       <p>商品名称:<input type="text" name="pname"></p>
       <p>商品价格:<input type="text" name="price"></p>
       <p>商品描述:<input type="text" name="pdesc"></p>
       <p>是否上架:<input type="text" name="pflag" placeholder="1上架/0下架"></p>
       <p>商品分类:
           <input type="text" name="cid" placeholder="1手机数码/2电脑办公/3运动鞋服/4图书音像">
       </p>
       <p><input type="submit" value="添加"></p>
   </form>
</body>
</html>

4.新增商品功能 

@WebServlet(name = "addProductServlet",urlPatterns = "/addProduct")
public class AddProductServlet extends HttpServlet {
    private ProductDao productDao = new ProductDao();
    private Product product = new Product();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            req.setCharacterEncoding("utf-8");
            String pid = req.getParameter("pid");
            product.setPid(pid);
            String pname = req.getParameter("pname");
            product.setPname(pname);
            String price = req.getParameter("price");
            product.setPrice(price);
            String pdesc = req.getParameter("pdesc");
            product.setPdesc(pdesc);
            String pflag = req.getParameter("pflag");
            product.setPflag(pflag);
            String cid = req.getParameter("cid");
            product.setCid(cid);
            int i = productDao.addProduct(product);
            if (i>0){
                System.out.println("数据插入成功");
                resp.sendRedirect("findAllProduct.html");
            } else {
                System.out.println("数据插入失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

5.查询商品页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查询数据</title>
</head>
<body>
<a href="findAllProduct">查询数据</a>
<a href="addProduct.html">返回插入页面</a>
<a href="index.html">返回主页</a>
<form action="findAllProduct" method="post">
</form>
</body>
</html>

6.查询商品功能

@WebServlet(name = "findAllProductServlet",urlPatterns = "/findAllProduct")
public class FindAllProductServlet extends HttpServlet {
    //由于需要访问ProductDao中的方法,所以将其作为成员变量
    private ProductDao productDao = new ProductDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //执行查询所有商品的操作
        List<Product> allProduct = null;
        try {
            allProduct = productDao.findAllProduct();
            //将allProduct集合写出到页面上
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.println("<table border=\"1\">");
            for (Product p :
                    allProduct) {
                writer.println("<tr><td>"+p.getPname()+"</td><td>"+p.getPrice()+"</td><td>"+p.getPdesc()+"</tr>");
            }
            writer.println("</table>");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}

 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值