首先,我们需要设计数据库表结构。根据需求,我们需要两个表:仓库表和物资台账明细表。
1.1 仓库表 (warehouse)
字段名 数据类型 描述
warehouse_id VARCHAR(10) 仓库编号(唯一)
name VARCHAR(50) 仓库名称
location VARCHAR(100) 仓库位置
capacity INT 仓库容量
created_at DATETIME 创建时间
1.2 物资台账明细表 (material_ledger)
字段名 数据类型 描述
ledger_id VARCHAR(20) 台账编号(唯一标识,格式:YYYYMMDD+顺序号)
material_id VARCHAR(10) 物资类别编码
operation_type ENUM('IN', 'OUT') 操作类别(入库或出库)
quantity INT 数量
unit VARCHAR(10) 计量单位
warehouse_id VARCHAR(10) 存放地点(仓库号)
operation_time DATETIME 操作时间
以下是登陆界面:
`
仓库管理系统登录
`登录功能处理:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/login")
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) && "admin123".equals(password)) {response.sendRedirect("adminDashboard.jsp");} else if ("warehouse".equals(username) && "warehouse123".equals(password)) {response.sendRedirect("warehouseDashboard.jsp");} else {response.sendRedirect("login.jsp?error=1");}
}
}
管理员功能界面:
管理员功能
- 新增仓库
- 删除仓库
- 修改仓库
- 新增物资类别
- 删除物资类别
- 修改物资类别
新增仓库:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/addWarehouse")
public class AddWarehouseServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String warehouseId = request.getParameter("warehouseId");
String name = request.getParameter("name");
String location = request.getParameter("location");
int capacity = Integer.parseInt(request.getParameter("capacity"));
// 调用DAO层进行数据库操作WarehouseDAO.addWarehouse(warehouseId, name, location, capacity);response.sendRedirect("adminDashboard.jsp");
}
}
入库操作:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/inboundOperation")
public class InboundOperationServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String materialId = request.getParameter("materialId");
int quantity = Integer.parseInt(request.getParameter("quantity"));
String unit = request.getParameter("unit");
String warehouseId = request.getParameter("warehouseId");
// 生成台账编号String ledgerId = generateLedgerId();// 调用DAO层进行数据库操作MaterialLedgerDAO.addMaterialLedger(ledgerId, materialId, "IN", quantity, unit, warehouseId);response.sendRedirect("warehouseDashboard.jsp");
}private String generateLedgerId() {// 生成台账编号的逻辑return "202402210001"; // 示例
}
}
数据库层操作:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class WarehouseDAO {
public static void addWarehouse(String warehouseId, String name, String location, int capacity) {
String sql = "INSERT INTO warehouse (warehouse_id, name, location, capacity) VALUES (?, ?, ?, ?)";
try (Connection conn = DatabaseUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, warehouseId);
pstmt.setString(2, name);
pstmt.setString(3, location);
pstmt.setInt(4, capacity);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
数据库连接工具:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseUtil {
private static final String URL = "jdbc:mysql://localhost:3306/warehouse_db";
private static final String USER = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() throws SQLException {return DriverManager.getConnection(URL, USER, PASSWORD);
}
}