1.ProductDao
package com.qiku.yrc.dao; import com.qiku.yrc.entity.Product; import com.qiku.yrc.utils.DruidUtils; import com.qiku.yrc.utils.DruidUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import java.sql.SQLException; import java.util.List; public class ProductDao { // Product product=new Product(); //查询所有商品 QueryRunner queryRunner= new QueryRunner(DruidUtils.dataSource); public List<Product> findAllProduct() throws SQLException { String sql = "select * from product"; List<Product> productList = queryRunner.query(sql, new BeanListHandler<Product>(Product.class)); return productList; } //新增商品 '12','iphonex','1999','山寨机就是牛','0','1' public int addProduct(Product product) throws SQLException { String sql="insert into product values(?,?,?,?,?,?)"; int update = queryRunner.update(sql, product.getCid(), product.getPname(), product.getPrice() , product.getPdesc(), product.getPflag(),product.getCid()); return update; } public List<Product> SelectProduct(Double price1 ,Double price2) throws SQLException { String sql ="select * from product where price between ? and ?"; List<Product> productList = queryRunner.query(sql, new BeanListHandler<Product>(Product.class),price1,price2); return productList; } public int updateProduct(Double price,String pid) throws SQLException { String sql="update product set price=? where pid=? "; int update = queryRunner.update(sql, price, pid); return update; } public void deleteProduct(String pid) throws SQLException { String sql="delete from product where pid = ?"; int update = queryRunner.update(sql, pid); } }
2.Product
package com.qiku.yrc.entity; /** * 商品表 对应 Product */ public class Product { private String pid; // 商品编号 private String pname;// 商品名称 private double price ;// 商品价格 private String pdesc; // 商品介绍 private int pflag ; // 商品状态 1 上架 , 0 下架 private String cid ;// 外键 对应分类表的主键 public Product() { } public Product(String pid, String pname, double price, String pdesc, int pflag, String cid) { this.pid = pid; this.pname = pname; this.price = price; this.pdesc = pdesc; this.pflag = pflag; this.cid = cid; } @Override public String toString() { return "Product{" + "pid='" + pid + '\'' + ", pname='" + pname + '\'' + ", price=" + price + ", pdesc='" + pdesc + '\'' + ", pflag=" + pflag + ", cid='" + cid + '\'' + '}'; } public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getPdesc() { return pdesc; } public void setPdesc(String pdesc) { this.pdesc = pdesc; } public int getPflag() { return pflag; } public void setPflag(int pflag) { this.pflag = pflag; } public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } }
主页
3.jsp页面
添加商品页面
<%@ page import="com.qiku.yrc.dao.ProductDao" %> <%@ page import="com.qiku.yrc.entity.Product" %> <%@ page import="java.sql.SQLException" %><%-- Created by IntelliJ IDEA. User: 13391 Date: 2022/5/14 Time: 11:31 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加的java页面</title> </head> <body> <%! private ProductDao productDao = new ProductDao(); %> <% request.setCharacterEncoding("utf-8"); String pid = request.getParameter("pid"); String pname = request.getParameter("pname"); double price = Double.parseDouble( request.getParameter("price") ); String pdesc = request.getParameter("pdesc"); int pflag = Integer.parseInt( request.getParameter("pflag") ); String cid = request.getParameter("cid"); Product product = new Product(pid, pname, price, pdesc, pflag, cid); // 将product 作为参数 传递给 ProduceDao 去存入数据库 try { productDao.addProduct(product); } catch (SQLException e) { e.printStackTrace(); } // 重定向到 查询页面 response.sendRedirect("selectAllProduct.jsp"); %>> </body> </html>
查询所有商品页面
<%@ page import="com.qiku.yrc.entity.Product" %> <%@ page import="java.util.List" %> <%@ page import="com.qiku.yrc.dao.ProductDao" %><%-- Created by IntelliJ IDEA. User: 13391 Date: 2022/5/14 Time: 10:53 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>查询所有商品页面</title> </head> <body> <form action="selectAllProduct.jsp"> <input type="submit" value="查询所有商品信息"> </form> <%-- 找到 Product数据库中的信息 将其私有化为成员变量--%> <%! private ProductDao productDao = new ProductDao(); %> <% request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); Cookie[] cookies = request.getCookies(); if (cookies != null){ for (Cookie cookie:cookies){ List<Product> productList = productDao.findAllProduct(); out.println("<table border='1' cellspacing='0' bordercolor='cyan'>"); out.println( "<tr><td>商品编号</td><td>商品名称</td>" + "<td>单价</td><td>描述</td><td>商品描述</td><td>类型id</td></tr>"); for (Product p:productList){ out.println("<tr><td>" + p.getPid() + "</td><td>" + p.getPname() + "</td><td>" + p.getPrice() + "</td><td>" + p.getPdesc() + "</td><td>" + p.getPflag() + "</td><td>" + p.getCid() + "</td></tr>"); } out.println("</table>"); out.flush(); out.close(); } } %> </body> </html>
删除商品信息页面
<%@ page import="com.qiku.yrc.dao.ProductDao" %><%-- Created by IntelliJ IDEA. User: 13391 Date: 2022/5/14 Time: 18:33 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>删除商品信息</title> </head> <body> <%! private ProductDao productDao = new ProductDao(); %> <% request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String pid = request.getParameter("pid"); productDao.deleteProduct(pid); response.sendRedirect("selectAllProduct.jsp"); %> </body> </html>
删除24 编号的 信息
修改商品信息
<%@ page import="com.qiku.yrc.dao.ProductDao" %><%-- Created by IntelliJ IDEA. User: 13391 Date: 2022/5/14 Time: 17:42 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%! private ProductDao productDao = new ProductDao(); %> <% request.setCharacterEncoding("utf-8"); String pid = request.getParameter("pid"); Double price=Double.parseDouble(request.getParameter("price")); productDao.updateProduct(price,pid); %> </body> </html>
根据句价格区间查询信息
<%@ page import="com.qiku.yrc.dao.ProductDao" %> <%@ page import="com.qiku.yrc.entity.Product" %> <%@ page import="java.util.List" %><%-- Created by IntelliJ IDEA. User: 13391 Date: 2022/5/14 Time: 16:24 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>查询商品</title> </head> <body> <%! private ProductDao productDao = new ProductDao(); // Product product = new Product(); %> <% request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); Double price1=Double.parseDouble(request.getParameter("price1")); Double price2=Double.parseDouble(request.getParameter("price2")); Cookie[] cookies = request.getCookies(); if (cookies != null){ for (Cookie cookie:cookies){ List<Product> productList = productDao.SelectProduct(price1,price2); out.println("<table border='1' cellspacing='0' bordercolor='cyan'>"); out.println( "<tr><td>商品编号</td><td>商品名称</td>" + "<td>单价</td><td>描述</td><td>商品描述</td><td>类型id</td></tr>"); for (Product p:productList){ out.println("<tr><td>" + p.getPid() + "</td><td>" + p.getPname() + "</td><td>" + p.getPrice() + "</td><td>" + p.getPdesc() + "</td><td>" + p.getPflag() + "</td><td>" + p.getCid() + "</td></tr>"); } out.println("</table>"); out.flush(); out.close(); } } %> </body> </html>