商品的增加删除修改和查询

该博客介绍了使用Java进行商品的增加、删除、修改和查询的实现。涉及GoodsDao、Result、Goods等类以及DruidUtils工具,通过Servlet处理AJAX请求,HTML页面展示商品数据。依赖管理使用了Maven。
摘要由CSDN通过智能技术生成

在这里插入图片描述
在这里插入图片描述
运行结果:
在这里插入图片描述

  • GoodsDao
package com.itheima.dao;

import com.itheima.pojo.Goods;
import com.itheima.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsDao {
   


   private QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
   public List<Goods>  queryAll() throws SQLException {
   

      String sql = "select * from t_goods";
      return qr.query(sql,new BeanListHandler<>(Goods.class));
   }
   public void addGoods(Goods goods) throws SQLException {
   
      String sql = "insert into t_goods values(?,?,?,?,?,?)";
      Object[] pamars = {
   null,goods.getName(),goods.getPrice(),
      goods.getStock(),goods.getDescription(),goods.getType_name()};
      qr.update(sql,pamars);
   }
   public int deleGoods(String id) throws SQLException {
   
      String sql = "delete from t_goods where id=?";
      return qr.update(sql, id);
   }
   public Goods findByIdGood(String id) throws SQLException {
   
      String sql = "select * from t_goods where id=?";
      return qr.query(sql,new BeanHandler<>(Goods.class),id);
   }
   public int updateGoods(Goods goods) throws SQLException {
   
      String sql = "update t_goods set NAME = ?" +
              ",price=?,stock=?,description=?,type_name=? where id = ?";
      Object[] pamars = {
   null,goods.getName(),goods.getPrice(),
              goods.getStock(),goods.getDescription(),goods.getDescription()};
      return qr.update(sql,pamars);
   }
}

  • Result
package com.itheima.entity;



/**
 * HTTP接口统一的返回数据结构
 **/
public class Result {
   
    private Boolean flag;//执行结果,true为执行成功 false为执行失败
    private String message;//返回结果信息
    private Object data;//返回数据


    public Boolean getFlag() {
   
        return flag;
    }

    public void setFlag(Boolean flag) {
   
        this.flag = flag;
    }

    public String getMessage() {
   
        return message;
    }

    public void setMessage(String message) {
   
        this.message = message;
    }

    public Object getData() {
   
        return data;
    }

    public void setData(Object data) {
   
        this.data = data;
    }

    public Result(Boolean flag, String message) {
   
        this.flag = flag;
        this.message = message;
    }

    public Result(Boolean flag, String message, Object data) {
   
        this.flag = flag;
        this.message = message;
        this.data = data;
    }
}
  • Goods
package com.itheima.pojo;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
public class Goods {
   
    private Integer id;
    private String name;
    private Double price;
    private Integer stock;
    private String description;
    private String type_name;

    public Goods() {
   
    }

    public Goods(Integer id, String name, Double price, Integer stock, String description, String type_name) {
   
        this.id = id;
        this.name = name;
        this.price = price;
        this.stock = stock;
        this.description = description;
        this.type_name = type_name;
    }

    public Integer getId() {
   
        return id;
    }

    public void setId(Integer id) {
   
        this.id = id;
    }

    public String getName() {
   
        return name;
    }

    public void setName(String name) {
   
        this.name = name;
    }

    public Double getPrice() {
   
        return price;
    }

    public void setPrice(Double price) {
   
        this.price = price;
    }

    public Integer getStock() {
   
        return stock;
    }

    public void setStock(Integer stock) {
   
        this.stock = stock;
    }

    public String getDescription() {
   
        return description;
    }

    public void setDescription(String description) {
   
        this.description = description;
    }

    public String getType_name() {
   
        return type_name;
    }

    public void setType_name(String type_name) {
   
        this.type_name = type_name;
    }

    @Override
    public String toString() {
   
        return "Goods{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", stock=" + stock +
                ", description='" + description + '\'' +
                ", type_name='" + type_name + '\'' +
                '}';
    }
}

  • GoodsService
package com.itheima.service;

import com.itheima.dao.GoodsDao;
import com.itheima.pojo.Goods;

import java.sql.SQLException;
import java.util.List;

public class GoodsService {
   

    private GoodsDao goodsDao = new GoodsDao();
    public List<Goods> queryAll(){
   
        try {
   
            return goodsDao.queryAll();

        } catch (SQLException e) {
   
            e.printStackTrace();
        }
        return null;
    }
    public boolean addGoods(Goods goods){
   
        try {
   
            goodsDao.addGoods(goods);
            return true;
        } catch (SQLException e) {
   
            e.printStackTrace();
        }
        return false;
    }
    public int deleGoods(String id){
   
        try {
   
           return goodsDao.deleGoods(id);
        } catch (SQLException e) {
   
            e.printStackTrace();
        }
        return 0;
    }
    public Goods findGoods(String id){
   
        try {
   
            return goodsDao.findByIdGood(id);
        } catch (SQLException e) {
   
            e.printStackTrace();
        }
        return null;
    }
    public int updateGoods(Goods goods){
   
        try {
   
            return goodsDao.updateGoods(goods);
        } catch (SQLException e) {
   
            e.printStackTrace();
        }
        return 0;
    }

}

  • DruidUtils
package com.itheima.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 阿里巴巴的连接池 Druid 工具类
 */
public class DruidUtils {
   
    /*
    1. 加载 druid.properties 配置文件
    2. 创建 Druid 连接池对象
    3. 提供 获得 连接池对象的方法
    4. 提供 从连接池中 获取连接对象Connection的 方法
    */

    public static DataSource ds = null;

    static {
   
        try {
   
            //1. 加载 druid.properties 配置文件
            InputStream is = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            Properties prop = new Properties();
            prop.load(is)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值