栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

5.13 作业

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

5.13 作业

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;
    }

}

主页

 

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.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 productList = productDao.findAllProduct(); out.println(""); out.println( "" + ""); for (Product p:productList){ out.println(""); } out.println("
商品编号商品名称单价描述商品描述类型id
" + p.getPid() + "" + p.getPname() + "" + p.getPrice() + "" + p.getPdesc() + "" + p.getPflag() + "" + p.getCid() + "
"); out.flush(); out.close(); // } } } // if (!b){ // Cookie cookie = new Cookie("select", "查询"); // response.addCookie(cookie); // } %>
 删除商品信息页面
<%@ 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(
                    "" +
                            "");
            for (Product p:productList){
                out.println("");
            }
            out.println("
商品编号商品名称单价描述商品描述类型id
" + p.getPid() + "" + p.getPname() + "" + p.getPrice() + "" + p.getPdesc() + "" + p.getPflag() + "" + p.getCid() + "
"); out.flush(); out.close(); // } } } %>

 

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/884994.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号