- 商品类及其Dao类
- 新增商品的jsp代码
- 查询商品信息的jsp代码
- 结果图
Product.java
package com.qiku.entity;
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;
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) {
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;
}
}
ProductDao.java
package com.qiku.Dao;
import com.qiku.entity.Product;
import com.qiku.utiles.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class ProductDao {
QueryRunner qr = new QueryRunner(DruidUtils.dataSource);
//查询所有商品信息
public List findAllProduct() throws SQLException {
String sql = "select * from product";
List productList = qr.query(sql, new BeanListHandler(Product.class));
return productList;
}
//新增一条商品信息
public int add(Product product) throws SQLException {
String pid = product.getPid();
String pname = product.getPname();
double price = product.getPrice();
String pdesc = product.getPdesc();
int pflag = product.getPflag();
String cid = product.getCid();
String sql = "insert into product (pid,pname,price,pdesc,pflag,cid) values (?,?,?,?,?,?)";
int update = qr.update(sql, pid, pname, price, pdesc,pflag,cid);
return update;
}
}
新增商品的jsp代码
addProduct.jsp
<%@ page import="com.qiku.Dao.ProductDao" %>
<%@ page import="com.qiku.entity.Product" %>
<%@ page import="com.qiku.utiles.DruidUtils" %>
<%@ page import="javax.sql.DataSource" %><%--
Created by IntelliJ IDEA.
User: cl
Date: 2022/5/13
Time: 19:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
新增页面
add.jsp
<%@ page import="com.qiku.Dao.ProductDao" %>
<%@ page import="com.qiku.entity.Product" %>
<%@ page import="java.sql.SQLException" %><%--
Created by IntelliJ IDEA.
User: cl
Date: 2022/5/13
Time: 20:55
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
addProduct重定向的目标
<%!
private ProductDao productDao = new ProductDao();
Product product = new Product();
%>
<%
request.setCharacterEncoding("utf-8");
product.setPid(request.getParameter("pid"));
product.setPname(request.getParameter("pname"));
product.setPrice(Double.parseDouble(request.getParameter("price")));
product.setPdesc(request.getParameter("pdesc"));
product.setPflag(Integer.parseInt(request.getParameter("pflag")));
product.setCid(request.getParameter("cid"));
try {
productDao.add(product);
} catch (SQLException e) {
e.printStackTrace();
}
response.sendRedirect("findAll.jsp");
%>
查询商品信息的jsp代码
findAll.jsp
<%@ page import="com.qiku.entity.Product" %>
<%@ page import="com.qiku.Dao.ProductDao" %>
<%@ page import="java.util.List" %><%--
Created by IntelliJ IDEA.
User: cl
Date: 2022/5/13
Time: 19:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
查询结果页面
商品查询操作
<%!
private ProductDao productDao = new ProductDao();
%>
<%
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
Cookie[] cookies = request.getCookies();
boolean b = false;
if (cookies != null){
for (Cookie c:cookies){
if (c.getName().equals("select")){
b = true;
List| 商品信息 | |||||
| 商品编号 | 商品名称 | " + "单价 | 描述 | pflag | 类型id |
| " + p.getPid() + " | " + p.getPname() + " | " + p.getPrice() + " | " + p.getPdesc() + " | " + p.getPflag() + " | " + p.getCid() + " |
主页图:
新增页面填充信息图:
提交后切换到查询页面图:
查询结果图:



