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 findAllProduct() throws SQLException {
String sql = "select * from product";
List productList = queryRunner.query(sql, new BeanListHandler(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 SelectProduct(Double price1 ,Double price2) throws SQLException {
String sql ="select * from product where price between ? and ?";
List productList = queryRunner.query(sql, new BeanListHandler(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;
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;
}
}
package com.qiku.yrc.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;
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" %>
添加的java页面
<%!
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");
%>>
<%@ 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" %>
添加的java页面
<%!
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");
%>>
查询所有商品页面
<%@ 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" %>
查询所有商品页面
<%-- 找到 Product数据库中的信息 将其私有化为成员变量--%>
<%!
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 cookie:cookies){
// if (c.getName().equals("select")){
// b = true;
List| 商品编号 | 商品名称 | " + "单价 | 描述 | 商品描述 | 类型id |
| " + p.getPid() + " | " + p.getPname() + " | " + p.getPrice() + " | " + p.getPdesc() + " | " + p.getPflag() + " | " + p.getCid() + " |
<%@ 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" %>
删除商品信息
<%!
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");
%>
删除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" %>
Title
<%!
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);
%>
根据句价格区间查询信息
<%@ 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" %>
查询商品
<%!
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();
// boolean b = false;
if (cookies != null){
for (Cookie cookie:cookies){
// if (c.getName().equals("select")){
// b = true;
List productList = productDao.SelectProduct(price1,price2);
out.println("");
out.println(
"商品编号 商品名称 " +
"单价 描述 商品描述 类型id ");
for (Product p:productList){
out.println("" + p.getPid() + " " + p.getPname() +
" " + p.getPrice() + " " + p.getPdesc() +
" " + p.getPflag() + " " + p.getCid() + " ");
}
out.println("
");
out.flush();
out.close();
// }
}
}
%>



