第二周总结:ai写编程
最近老师留下了一个使用ai来编程的任务,
采用java写一个网页版的仓储管理系统原型系统,不要使用mybatis和vue技术和util,使用mysql和jdbc等技术,采用前端,servlet,dao层来进行。一共包含三个数据表:仓库表,物资台账明细表和物资类别表,仓库表(支持多个仓库,每个仓库具有唯一编码);三个表有数据的关联,比如物资是存放在仓库的,;物资台账需包含物资编码、物资名称、规格、材质、供应商、品牌、物资分类,用户可以自定义物资的物资分类,要求不同的物资名称、规格、材质的物资不能设立相同的物资编码。物资台账明细表(除了需求描述的要求外,需要有台账编号(唯一标识,四位年两位月两位日+顺序号(四位)例如:202402210022),操作类别(入库或者出库)数量,计量单位,存放地点(仓库号)等基本信息);物资类别表:需要满足不同的物资名称、规格、材质的物资不能设立相同的物资编码,还要有相关数量和存放在哪个仓库的位置信息;要求有登录页面,管理员、仓库管理人员统一登录界面登录后,显示各自不同的菜单项。管理员功能页:管理员可以实现仓库表的增删改,但是只有物资表中没有物资存放的仓库才可以进行删除和修改,可以实现对物资表内数据的新增、删除、修改,物资表中某项物资数量默认为0,进行出库入库的操作时数量会进行变动,物资数量为0时才可以进行删除和修改;仓库管理人员:入库操作,出库操作,统计查询功能。要求出库物资少于仓库物资无法出库,请给出详细源代码和精简的中文注释,前端页面要求中文,这是我主要的问询语句,在此之外还有很多补充和先前提问作为铺垫,使用的主要ai是chatgpt,兼用了deepseek
以下是我让ai生成的源码和感想
首先,ai有偷工减料现象,让它生成代码,它总是会只生成一部分所谓的关键部分,省略一部分;其次,ai会的和我们会的不一样,ai自由发挥很容易生成一些用其他不熟悉的技术的不明所以的代码,所以要指定ai用你会的技术来编程;然后ai有“忘性”,它会忘掉之前的东西,你给它太多东西,它生成不出来,给它的少了,它又没法子和前面的东西联系起来。这就要求对于项目有一个全面深入的理解,在询问时,对于环境,技术和其他方面都要做到详尽,除此之外,还要做好后期的修改和调试操作,这样才能更好地利用ai编程
以下是部分源码
登录页面
用户控制面板
管理仓库
| ID | 编码 | 名称 | 地址 | 容量 | 操作 |
|---|---|---|---|---|---|
| ${warehouse.warehouseId} | ${warehouse.warehouseCode} | ${warehouse.warehouseName} | ${warehouse.address} | ${warehouse.capacity} | 编辑 删除 |
管理物资
| ID | 编码 | 名称 | 规格 | 类型 | 供应商 | 品牌 | 分类ID | 数量 | 单位 | 存放位置 | 操作 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| ${material.materialId} | ${material.materialCode} | ${material.materialName} | ${material.specification} | ${material.materialType} | ${material.supplier} | ${material.brand} | ${material.categoryId} | ${material.quantity} | ${material.unit} | ${material.location} | 编辑 删除 |
编辑仓库
管理员控制面板
package org.example.storagesys;import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class WarehouseDAO {
public Connection getConnection()throws SQLException ,ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/testsystem";
String user = "root";
String password = "123456";
return DriverManager.getConnection(url, user, password);
}
public void addWarehouse(Warehouse warehouse) throws SQLException, ClassNotFoundException {
String sql = "INSERT INTO warehouse (warehouse_code, warehouse_name, address, capacity) VALUES (?, ?, ?, ?)";
Connection conn =getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, warehouse.getWarehouseCode());
ps.setString(2, warehouse.getWarehouseName());
ps.setString(3, warehouse.getAddress());
ps.setInt(4, warehouse.getCapacity());
ps.executeUpdate();
}
public void deleteWarehouse(String warehouseCode) throws SQLException, ClassNotFoundException {
String sql = "DELETE FROM warehouse WHERE warehouse_code = ?";
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, warehouseCode);
ps.executeUpdate();
}
public void updateWarehouse(Warehouse warehouse) throws SQLException, ClassNotFoundException {
String sql = "UPDATE warehouse SET warehouse_name = ?, address = ?, capacity = ? WHERE warehouse_code = ?";
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, warehouse.getWarehouseName());
ps.setString(2, warehouse.getAddress());
ps.setInt(3, warehouse.getCapacity());
ps.setString(4, warehouse.getWarehouseCode());
ps.executeUpdate();
}
public Warehouse getWarehouseByCode(String warehouseCode) throws SQLException, ClassNotFoundException {
String sql = "SELECT * FROM warehouse WHERE warehouse_code = ?";
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, warehouseCode);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Warehouse warehouse = new Warehouse();
warehouse.setWarehouseId(rs.getInt("warehouse_id"));
warehouse.setWarehouseCode(rs.getString("warehouse_code"));
warehouse.setWarehouseName(rs.getString("warehouse_name"));
warehouse.setAddress(rs.getString("address"));
warehouse.setCapacity(rs.getInt("capacity"));
return warehouse;
}
return null;
}
public List<Warehouse> getAllWarehouses() throws SQLException, ClassNotFoundException {
String sql = "SELECT * FROM warehouse";
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
List<Warehouse> warehouses = new ArrayList<>();
while (rs.next()) {
Warehouse warehouse = new Warehouse();
warehouse.setWarehouseId(rs.getInt("warehouse_id"));
warehouse.setWarehouseCode(rs.getString("warehouse_code"));
warehouse.setWarehouseName(rs.getString("warehouse_name"));
warehouse.setAddress(rs.getString("address"));
warehouse.setCapacity(rs.getInt("capacity"));
warehouses.add(warehouse);
}
return warehouses;
}
}
package org.example.storagesys;
public class Warehouse {
private int warehouseId;
private String warehouseCode;
private String warehouseName;
private String address;
private int capacity;
public Warehouse() {}
public Warehouse(String warehouseCode, String warehouseName, String address, int capacity) {
this.warehouseCode = warehouseCode;
this.warehouseName = warehouseName;
this.address = address;
this.capacity = capacity;
}
public int getWarehouseId() {
return warehouseId;
}
public void setWarehouseId(int warehouseId) {
this.warehouseId = warehouseId;
}
public String getWarehouseCode() {
return warehouseCode;
}
public void setWarehouseCode(String warehouseCode) {
this.warehouseCode = warehouseCode;
}
public String getWarehouseName() {
return warehouseName;
}
public void setWarehouseName(String warehouseName) {
this.warehouseName = warehouseName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
}
package org.example.storagesys;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
HttpSession session = request.getSession();
if ("logout".equals(action)) {
session.invalidate();
response.sendRedirect("index.jsp");
} else if ("addMaterial".equals(action)) {
String materialCode = request.getParameter("materialCode");
String materialName = request.getParameter("materialName");
String specification = request.getParameter("specification");
String materialType = request.getParameter("materialType");
String supplier = request.getParameter("supplier");
String brand = request.getParameter("brand");
int categoryId = Integer.parseInt(request.getParameter("categoryId"));
int quantity = Integer.parseInt(request.getParameter("quantity"));
String unit = request.getParameter("unit");
String location = request.getParameter("location");
Material material = new Material(materialCode, materialName, specification, materialType, supplier, brand, categoryId);
MaterialDAO dao = new MaterialDAO();
try {
dao.addMaterial(material);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
MaterialDetailDAO detailDao = new MaterialDetailDAO();
MaterialDetail detail = new MaterialDetail(materialCode, "入库", quantity, unit, location);
detailDao.addMaterialDetail(detail);
response.sendRedirect("user.jsp");
} else if ("removeMaterial".equals(action)) {
String materialCode = request.getParameter("materialCode");
MaterialDAO dao = new MaterialDAO();
dao.deleteMaterial(materialCode);
response.sendRedirect("user.jsp");
} else if ("queryMaterial".equals(action)) {
String materialCode = request.getParameter("materialCode");
MaterialDAO dao = new MaterialDAO();
Material material = dao.getMaterialByCode(materialCode);
request.setAttribute("material", material);
request.getRequestDispatcher("user_material.jsp").forward(request, response);
} else if ("inbound".equals(action)) {
String materialCode = request.getParameter("materialCode");
int quantity = Integer.parseInt(request.getParameter("quantity"));
String unit = request.getParameter("unit");
String location = request.getParameter("location");
MaterialDAO dao = new MaterialDAO();
Material material = dao.getMaterialByCode(materialCode);
if (material != null) {
MaterialDetailDAO detailDao = new MaterialDetailDAO();
MaterialDetail detail = new MaterialDetail(materialCode, "入库", quantity, unit, location);
detailDao.addMaterialDetail(detail);
response.sendRedirect("user.jsp");
} else {
response.sendRedirect("user.jsp?error=true");
}
} else if ("outbound".equals(action)) {
String materialCode = request.getParameter("materialCode");
int quantity = Integer.parseInt(request.getParameter("quantity"));
String unit = request.getParameter("unit");
String location = request.getParameter("location");
MaterialDAO dao = new MaterialDAO();
Material material = null;
try {
material = dao.getMaterialByCode(materialCode);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
if (material != null && material.getQuantity() >= quantity) {
MaterialDetailDAO detailDao = new MaterialDetailDAO();
MaterialDetail detail = new MaterialDetail(materialCode, "出库", quantity, unit, location);
detailDao.addMaterialDetail(detail);
response.sendRedirect("user.jsp");
} else {
response.sendRedirect("user.jsp?error=true");
}
} else if ("queryInventory".equals(action)) {
String materialCode = request.getParameter("materialCode");
MaterialDAO dao = new MaterialDAO();
Material material = null;
try {
material = dao.getMaterialByCode(materialCode);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
request.setAttribute("material", material);
request.getRequestDispatcher("user_inventory.jsp").forward(request, response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package org.example.storagesys;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class MaterialDetailDAO {
private Connection connection;
public MaterialDetailDAO(Connection connection) {
this.connection = connection;
}
public List<MaterialDetail> getAllDetails() throws SQLException {
List<MaterialDetail> details = new ArrayList<>();
String sql = "SELECT md.*, w.warehouse_name FROM material_detail md JOIN warehouse w ON md.location = w.warehouse_code";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
MaterialDetail detail = new MaterialDetail(
resultSet.getInt("detail_id"),
resultSet.getString("inventory_number"),
resultSet.getString("operation_type"),
resultSet.getInt("quantity"),
resultSet.getString("unit"),
resultSet.getString("location"),
resultSet.getString("warehouse_name")
);
details.add(detail);
}
return details;
}
// 其他方法如 add, update, delete 可以类似地实现
}
package org.example.storagesys;
public class MaterialDetail {
private String inventoryNumber;
private String materialName;
private String specification;
private String materialType;
private String supplier;
private String brand;
private int categoryId;
private int quantity;
private String unit;
private String location;
public MaterialDetail(String inventoryNumber, String materialName, String specification, String materialType, String supplier, String brand, int categoryId, int quantity, String unit, String location) {
this.inventoryNumber = inventoryNumber;
this.materialName = materialName;
this.specification = specification;
this.materialType = materialType;
this.supplier = supplier;
this.brand = brand;
this.categoryId = categoryId;
this.quantity = quantity;
this.unit = unit;
this.location = location;
}
public String getInventoryNumber() {
return inventoryNumber;
}
public void setInventoryNumber(String inventoryNumber) {
this.inventoryNumber = inventoryNumber;
}
public String getMaterialName() {
return materialName;
}
public void setMaterialName(String materialName) {
this.materialName = materialName;
}
public String getSpecification() {
return specification;
}
public void setSpecification(String specification) {
this.specification = specification;
}
public String getMaterialType() {
return materialType;
}
public void setMaterialType(String materialType) {
this.materialType = materialType;
}
public String getSupplier() {
return supplier;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
package org.example.storagesys;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class MaterialDAO {
public Connection getConnection()throws SQLException ,ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/testsystem";
String user = "root";
String password = "123456";
return DriverManager.getConnection(url, user, password);
}
public void addMaterial(Material material) throws SQLException, ClassNotFoundException {
String sql = "INSERT INTO material (material_code, material_name, specification, material_type, supplier, brand, category_id) VALUES (?, ?, ?, ?, ?, ?, ?)";
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, material.getMaterialCode());
ps.setString(2, material.getMaterialName());
ps.setString(3, material.getSpecification());
ps.setString(4, material.getMaterialType());
ps.setString(5, material.getSupplier());
ps.setString(6, material.getBrand());
ps.setInt(7, material.getCategoryId());
ps.executeUpdate();
}
public void deleteMaterial(String materialCode) throws SQLException, ClassNotFoundException {
String sql = "DELETE FROM material WHERE material_code = ?";
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, materialCode);
ps.executeUpdate();
}
public void updateMaterial(Material material) throws SQLException, ClassNotFoundException {
String sql = "UPDATE material SET material_name = ?, specification = ?, material_type = ?, supplier = ?, brand = ?, category_id = ? WHERE material_code = ?";
Connection conn =getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, material.getMaterialName());
ps.setString(2, material.getSpecification());
ps.setString(3, material.getMaterialType());
ps.setString(4, material.getSupplier());
ps.setString(5, material.getBrand());
ps.setInt(6, material.getCategoryId());
ps.setString(7, material.getMaterialCode());
ps.executeUpdate();
}
public Material getMaterialByCode(String materialCode) throws SQLException, ClassNotFoundException {
String sql = "SELECT * FROM material WHERE material_code = ?";
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, materialCode);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Material material = new Material();
material.setMaterialId(rs.getInt("material_id"));
material.setMaterialCode(rs.getString("material_code"));
material.setMaterialName(rs.getString("material_name"));
material.setSpecification(rs.getString("specification"));
material.setMaterialType(rs.getString("material_type"));
material.setSupplier(rs.getString("supplier"));
material.setBrand(rs.getString("brand"));
material.setCategoryId(rs.getInt("category_id"));
return material;
}
return null;
}
public List<Material> getAllMaterials() throws SQLException, ClassNotFoundException {
String sql = "SELECT * FROM material";
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
List<Material> materials = new ArrayList<>();
while (rs.next()) {
Material material = new Material();
material.setMaterialId(rs.getInt("material_id"));
material.setMaterialCode(rs.getString("material_code"));
material.setMaterialName(rs.getString("material_name"));
material.setSpecification(rs.getString("specification"));
material.setMaterialType(rs.getString("material_type"));
material.setSupplier(rs.getString("supplier"));
material.setBrand(rs.getString("brand"));
material.setCategoryId(rs.getInt("category_id"));
materials.add(material);
}
return materials;
}
}
package org.example.storagesys;
public class Material {
private int materialId;
private String materialCode;
private String materialName;
private String specification;
private String materialType;
private String supplier;
private String brand;
private int categoryId;
private int quantity;
private String unit;
private String location;
public Material() {}
public Material(String materialCode, String materialName, String specification, String materialType, String supplier, String brand, int categoryId) {
this.materialCode = materialCode;
this.materialName = materialName;
this.specification = specification;
this.materialType = materialType;
this.supplier = supplier;
this.brand = brand;
this.categoryId = categoryId;
}
public int getMaterialId() {
return materialId;
}
public void setMaterialId(int materialId) {
this.materialId = materialId;
}
public String getMaterialCode() {
return materialCode;
}
public void setMaterialCode(String materialCode) {
this.materialCode = materialCode;
}
public String getMaterialName() {
return materialName;
}
public void setMaterialName(String materialName) {
this.materialName = materialName;
}
public String getSpecification() {
return specification;
}
public void setSpecification(String specification) {
this.specification = specification;
}
public String getMaterialType() {
return materialType;
}
public void setMaterialType(String materialType) {
this.materialType = materialType;
}
public String getSupplier() {
return supplier;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
package org.example.storagesys;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// 验证用户名和密码(这里简化处理)
if ("admin".equals(username) && "admin".equals(password)) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect("admin.jsp");
} else {
response.sendRedirect("index.jsp?error=true");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
package org.example.storagesys;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
HttpSession session = request.getSession();
if ("logout".equals(action)) {
session.invalidate();
response.sendRedirect("index.jsp");
} else if ("manageWarehouses".equals(action)) {
try {
WarehouseDAO dao = new WarehouseDAO();
List<Warehouse> warehouses = dao.getAllWarehouses();
request.setAttribute("warehouses", warehouses);
request.getRequestDispatcher("admin_warehouses.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else if ("manageMaterials".equals(action)) {
try {
MaterialDAO dao = new MaterialDAO();
List<Material> materials = dao.getAllMaterials();
request.setAttribute("materials", materials);
request.getRequestDispatcher("admin_materials.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
} else if ("addWarehouse".equals(action)) {
String warehouseCode = request.getParameter("warehouseCode");
String warehouseName = request.getParameter("warehouseName");
String address = request.getParameter("address");
int capacity = Integer.parseInt(request.getParameter("capacity"));
Warehouse warehouse = new Warehouse(warehouseCode, warehouseName, address, capacity);
WarehouseDAO dao = new WarehouseDAO();
try {
dao.addWarehouse(warehouse);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
response.sendRedirect("admin.jsp?action=manageWarehouses");
} else if ("deleteWarehouse".equals(action)) {
String warehouseCode = request.getParameter("warehouseCode");
WarehouseDAO dao = new WarehouseDAO();
try {
dao.deleteWarehouse(warehouseCode);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
response.sendRedirect("admin.jsp?action=manageWarehouses");
} else if ("editWarehouse".equals(action)) {
String warehouseCode = request.getParameter("warehouseCode");
WarehouseDAO dao = new WarehouseDAO();
Warehouse warehouse = null;
try {
warehouse = dao.getWarehouseByCode(warehouseCode);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
request.setAttribute("warehouse", warehouse);
request.getRequestDispatcher("admin_edit_warehouse.jsp").forward(request, response);
} else if ("updateWarehouse".equals(action)) {
String warehouseCode = request.getParameter("warehouseCode");
String warehouseName = request.getParameter("warehouseName");
String address = request.getParameter("address");
int capacity = Integer.parseInt(request.getParameter("capacity"));
Warehouse warehouse = new Warehouse(warehouseCode, warehouseName, address, capacity);
WarehouseDAO dao = new WarehouseDAO();
try {
dao.updateWarehouse(warehouse);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
response.sendRedirect("admin.jsp?action=manageWarehouses");
} else if ("addMaterial".equals(action)) {
String materialCode = request.getParameter("materialCode");
String materialName = request.getParameter("materialName");
String specification = request.getParameter("specification");
String materialType = request.getParameter("materialType");
String supplier = request.getParameter("supplier");
String brand = request.getParameter("brand");
int categoryId = Integer.parseInt(request.getParameter("categoryId"));
Material material = new Material(materialCode, materialName, specification, materialType, supplier, brand, categoryId);
MaterialDAO dao = new MaterialDAO();
try {
dao.addMaterial(material);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
response.sendRedirect("admin.jsp?action=manageMaterials");
} else if ("deleteMaterial".equals(action)) {
String materialCode = request.getParameter("materialCode");
MaterialDAO dao = new MaterialDAO();
try {
dao.deleteMaterial(materialCode);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
response.sendRedirect("admin.jsp?action=manageMaterials");
} else if ("editMaterial".equals(action)) {
String materialCode = request.getParameter("materialCode");
MaterialDAO dao = new MaterialDAO();
Material material = null;
try {
material = dao.getMaterialByCode(materialCode);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
request.setAttribute("material", material);
request.getRequestDispatcher("admin_edit_material.jsp").forward(request, response);
} else if ("updateMaterial".equals(action)) {
String materialCode = request.getParameter("materialCode");
String materialName = request.getParameter("materialName");
String specification = request.getParameter("specification");
String materialType = request.getParameter("materialType");
String supplier = request.getParameter("supplier");
String brand = request.getParameter("brand");
int categoryId = Integer.parseInt(request.getParameter("categoryId"));
Material material = new Material(materialCode, materialName, specification, materialType, supplier, brand, categoryId);
MaterialDAO dao = new MaterialDAO();
try {
dao.updateMaterial(material);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
response.sendRedirect("admin.jsp?action=manageMaterials");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}

浙公网安备 33010602011771号