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

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

  • 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 Pro
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码如下: ```c #include <stdio.h> #include <string.h> #define MAX_LEN 50 #define MAX_PRODUCTS 20 typedef struct { char id[MAX_LEN]; char name[MAX_LEN]; double price; } Product; int find_product_index(Product products[], int num_products, char id[]) { for (int i = 0; i < num_products; i++) { if (strcmp(products[i].id, id) == 0) { return i; } } return -1; } void add_product(Product products[], int *num_products) { if (*num_products == MAX_PRODUCTS) { printf("Error: Maximum number of products reached!\n"); return; } Product new_product; printf("Enter product ID: "); scanf("%s", new_product.id); printf("Enter product name: "); scanf("%s", new_product.name); printf("Enter product price: "); scanf("%lf", &new_product.price); products[*num_products] = new_product; (*num_products)++; printf("Product added successfully!\n"); } void delete_product(Product products[], int *num_products) { char id[MAX_LEN]; printf("Enter product ID to delete: "); scanf("%s", id); int index = find_product_index(products, *num_products, id); if (index == -1) { printf("Error: Product not found!\n"); return; } for (int i = index; i < (*num_products) - 1; i++) { products[i] = products[i + 1]; } (*num_products)--; printf("Product deleted successfully!\n"); } void modify_product(Product products[], int num_products) { char id[MAX_LEN]; printf("Enter product ID to modify: "); scanf("%s", id); int index = find_product_index(products, num_products, id); if (index == -1) { printf("Error: Product not found!\n"); return; } Product modified_product; printf("Enter new product name (press enter to skip): "); scanf("%s", modified_product.name); printf("Enter new product price (press 0 to skip): "); scanf("%lf", &modified_product.price); if (strlen(modified_product.name) > 0) { strcpy(products[index].name, modified_product.name); } if (modified_product.price != 0) { products[index].price = modified_product.price; } printf("Product modified successfully!\n"); } void display_product(Product product) { printf("%-10s %-20s $%.2lf\n", product.id, product.name, product.price); } void display_all_products(Product products[], int num_products) { printf("%-10s %-20s %s\n", "ID", "Name", "Price"); for (int i = 0; i < num_products; i++) { display_product(products[i]); } } int main() { Product products[MAX_PRODUCTS]; int num_products = 0; int choice; do { printf("\n1. Add product\n"); printf("2. Delete product\n"); printf("3. Modify product\n"); printf("4. Display all products\n"); printf("0. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: add_product(products, &num_products); break; case 2: delete_product(products, &num_products); break; case 3: modify_product(products, num_products); break; case 4: display_all_products(products, num_products); break; case 0: printf("Exiting program...\n"); break; default: printf("Invalid choice!\n"); } } while (choice != 0); return 0; } ``` 这个程序定义了一个 `Product` 结构体,包含商品的 ID、名称和价格。然后实现了增加商品、按学号删除商品修改商品信息和查询商品信息的功能。其中,查询商品信息的功能是通过 `display_all_products` 函数实现的,该函数将所有商品的信息输出到控制台上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值