超市商品增删改查管理系统开发教程

本教程将带你一步步构建一个简单的超市商品管理系统,实现对商品信息的增删改查功能。

1. 项目准备

  1. 新建一个名为 'shop' 的 Web 工程。
  2. 建立数据库商品表 'goods',包含以下字段:

| 字段名 | 类型 | 说明 | |---|---|---| | 编号 | int | 商品唯一标识,自动递增 | | 商品名 | varchar | 商品名称 | | 商品种类 | varchar | 商品种类,如'蔬菜'、'乳制品'、'肉蛋'等 | | 商品价格 | double | 商品价格 | | 单位 | varchar | 商品单位,如'斤'、'袋'、'箱'等 | | 更新时间 | datetime | 商品信息最后更新时间 |

2. 编写代码

2.1 实体类 (Good.java)

public class Good {
    private int id;
    private String name;
    private String category;
    private double price;
    private String unit;
    private Date updateTime;
    
    // constructors, getters and setters
}

2.2 数据访问层 (GoodDao.java)

public class GoodDao {
    private Connection connection;
    
    public GoodDao() {
        // initialize database connection
    }
    
    public void addGood(Good good) {
        // insert the good into the database
    }
    
    public void deleteGood(int id) {
        // delete the good from the database by id
    }
    
    public void updateGood(Good good) {
        // update the good in the database
    }
    
    public Good getGoodById(int id) {
        // retrieve the good from the database by id
        // return the Good object
    }
}

2.3 Servlet (GoodServlet.java)

@WebServlet("/goods")
public class GoodServlet extends HttpServlet {
    private GoodDao goodDao;
    
    public void init() {
        goodDao = new GoodDao();
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // handle addGood request
        String name = request.getParameter('name');
        String category = request.getParameter('category');
        double price = Double.parseDouble(request.getParameter('price'));
        String unit = request.getParameter('unit');
        Date updateTime = new Date();
        
        Good good = new Good(name, category, price, unit, updateTime);
        goodDao.addGood(good);
        
        // redirect to success page
        response.sendRedirect('success.jsp');
    }
    
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // handle deleteGood request
        int id = Integer.parseInt(request.getParameter('id'));
        goodDao.deleteGood(id);
        
        // send success response
        response.setStatus(HttpServletResponse.SC_OK);
    }
    
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // handle updateGood request
        int id = Integer.parseInt(request.getParameter('id'));
        String name = request.getParameter('name');
        String category = request.getParameter('category');
        double price = Double.parseDouble(request.getParameter('price'));
        String unit = request.getParameter('unit');
        Date updateTime = new Date();
        
        Good good = new Good(id, name, category, price, unit, updateTime);
        goodDao.updateGood(good);
        
        // send success response
        response.setStatus(HttpServletResponse.SC_OK);
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // handle getGoodById request
        int id = Integer.parseInt(request.getParameter('id'));
        Good good = goodDao.getGoodById(id);
        
        // set response content type
        response.setContentType('application/json');
        
        // convert Good object to JSON string
        String json = new Gson().toJson(good);
        
        // send JSON response
        response.getWriter().write(json);
    }
}

2.4 添加商品页面 (addGood.jsp)

<html>
<body>
    <form action='goods' method='post'>
        <label for='name'>Name:</label>
        <input type='text' id='name' name='name'><br><br>
        
        <label for='category'>Category:</label>
        <select id='category' name='category'>
            <option value='蔬菜'>蔬菜</option>
            <option value='乳制品'>乳制品</option>
            <option value='肉蛋'>肉蛋</option>
            <option value='饮料'>饮料</option>
            <option value='零食'>零食</option>
            <option value='主食'>主食</option>
        </select><br><br>
        
        <label for='price'>Price:</label>
        <input type='number' id='price' name='price' step='0.01'><br><br>
        
        <label for='unit'>Unit:</label>
        <input type='text' id='unit' name='unit'><br><br>
        
        <input type='submit' value='Submit'>
    </form>
</body>
</html>

2.5 成功提示页面 (success.jsp)

<html>
<body>
    <h1>Success!</h1>
    <p>The good has been added successfully.</p>
</body>
</html>

3. 运行测试

完成以上步骤后,运行项目,通过浏览器访问添加商品页面,输入商品信息并提交,即可将商品数据保存到数据库。

总结

本教程介绍了如何使用Java Web开发一个简单的超市商品管理系统。通过本教程的学习,你可以掌握以下知识:

  • Java Web开发基础
  • 数据库设计与操作
  • Servlet、JSP、HTML页面开发
  • 数据访问层设计
  • 实体类设计

希望本教程能够帮助你快速入门Java Web开发,并顺利完成你的项目。

超市商品增删改查管理系统开发教程

原文地址: https://www.cveoy.top/t/topic/o6by 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录