用Java,MySQL数据库写
时间: 2024-11-17 17:30:51 浏览: 19
为了实现一个基于Java和MySQL的仓库管理系统,你需要完成以下几个主要步骤:
### 1. 环境准备
- **依赖管理**:Maven 或 Gradle
### 2. 表结构设计
根据文档中的描述,需要设计以下表结构:
#### 用户表 (users)
```sql
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
);
```
#### 仓库表 (warehouses)
```sql
CREATE TABLE warehouses (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
capacity INT NOT NULL,
address VARCHAR(255) NOT NULL
);
```
#### 库存表 (inventories)
```sql
CREATE TABLE inventories (
id INT AUTO_INCREMENT PRIMARY KEY,
warehouse_id INT,
type ENUM('常温', '低温', '高温') NOT NULL,
max_capacity INT NOT NULL,
used_capacity INT NOT NULL DEFAULT 0,
warning_capacity INT NOT NULL,
FOREIGN KEY (warehouse_id) REFERENCES warehouses(id)
);
```
#### 产品表 (products)
```sql
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
product_code VARCHAR(50) NOT NULL UNIQUE,
quantity INT NOT NULL,
expiration_date DATE NOT NULL
);
```
#### 库存明细表 (inventory_details)
```sql
CREATE TABLE inventory_details (
id INT AUTO_INCREMENT PRIMARY KEY,
inventory_id INT,
product_id INT,
quantity INT NOT NULL,
FOREIGN KEY (inventory_id) REFERENCES inventories(id),
FOREIGN KEY (product_id) REFERENCES products(id)
);
```
### 3. Java代码实现
#### 1. 数据访问层 (DAO)
使用JDBC连接MySQL数据库,编写DAO类来处理数据库操作。
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class WarehouseDAO {
private static final String URL = "jdbc:mysql://localhost:3306/warehouse";
private static final String USER = "root";
private static final String PASSWORD = "password";
public Connection getConnection() throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(URL, USER, PASSWORD);
}
// 示例方法:查询所有仓库
public List<Warehouse> getAllWarehouses() {
List<Warehouse> warehouses = new ArrayList<>();
try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM warehouses")) {
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Warehouse warehouse = new Warehouse(
rs.getInt("id"),
rs.getString("name"),
rs.getInt("capacity"),
rs.getString("address")
);
warehouses.add(warehouse);
}
} catch (Exception e) {
e.printStackTrace();
}
return warehouses;
}
}
```
#### 2. 业务逻辑层 (Service)
编写Service类来处理业务逻辑。
```java
public class WarehouseService {
private WarehouseDAO warehouseDAO = new WarehouseDAO();
public List<Warehouse> get Warehouses() {
return warehouseDAO.getAllWarehouses();
}
// 其他业务方法...
}
```
#### 3. 控制台界面
编写主程序类,通过控制台与用户交互。
```java
import java.util.Scanner;
public class Main {
private static WarehouseService warehouseService = new WarehouseService();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
boolean running = true;
while (running) {
System.out.println("1. 注册");
System.out.println("2. 登录");
System.out.println("3. 仓库管理");
System.out.println("4. 库存管理");
System.out.println("5. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
registerUser();
break;
case 2:
loginUser();
break;
case 3:
manageWarehouse();
break;
case 4:
manageInventory();
break;
case 5:
running = false;
break;
default:
System.out.println("无效的选择,请重新输入。");
}
}
}
private static void registerUser() {
System.out.print("请输入用户名:");
String username = scanner.next();
System.out.print("请输入密码:");
String password = scanner.next();
System.out.print("请确认密码:");
String confirmPassword = scanner.next();
if (!password.equals(confirmPassword)) {
System.out.println("密码不一致,请重新输入。");
return;
}
// 调用UserService.register(username, password);
System.out.println("注册成功!");
}
private static void loginUser() {
System.out.print("请输入用户名:");
String username = scanner.next();
System.out.print("请输入密码:");
String password = scanner.next();
// 调用UserService.login(username, password);
System.out.println("登录成功!");
}
private static void manageWarehouse() {
boolean running = true;
while (running) {
System.out.println("1. 查询仓库");
System.out.println("2. 新建仓库");
System.out.println("3. 修改仓库");
System.out.println("4. 删除仓库");
System.out.println("5. 返回上级菜单");
int choice = scanner.nextInt();
switch (choice) {
case 1:
queryWarehouses();
break;
case 2:
createWarehouse();
break;
case 3:
updateWarehouse();
break;
case 4:
deleteWarehouse();
break;
case 5:
running = false;
break;
default:
System.out.println("无效的选择,请重新输入。");
}
}
}
private static void queryWarehouses() {
List<Warehouse> warehouses = warehouseService.getWarehouses();
for (Warehouse warehouse : warehouses) {
System.out.println(warehouse);
}
}
private static void createWarehouse() {
System.out.print("请输入仓库名称:");
String name = scanner.next();
System.out.print("请输入仓库容量:");
int capacity = scanner.nextInt();
System.out.print("请输入仓库地址:");
String address = scanner.next();
// 调用WarehouseService.createWarehouse(name, capacity, address);
System.out.println("新建仓库成功!");
}
private static void updateWarehouse() {
System.out.print("请输入要修改的仓库ID:");
int id = scanner.nextInt();
System.out.print("请输入新的仓库名称:");
String name = scanner.next();
System.out.print("请输入新的仓库容量:");
int capacity = scanner.nextInt();
System.out.print("请输入新的仓库地址:");
String address = scanner.next();
// 调用WarehouseService.updateWarehouse(id, name, capacity, address);
System.out.println("修改仓库成功!");
}
private static void deleteWarehouse() {
System.out.print("请输入要删除的仓库ID:");
int id = scanner.nextInt();
// 调用WarehouseService.deleteWarehouse(id);
System.out.println("删除仓库成功!");
}
private static void manageInventory() {
// 实现库存管理相关功能
}
}
```
### 4. 功能扩展
根据文档中的详细功能列表,逐步实现其他功能模块,如库存管理、产品质量预警等。
### 5. 测试
编写单元测试和集成测试,确保各个功能模块正常工作。
### 6. 部署
将应用部署到服务器,确保其稳定运行。
以上是基于Java和MySQL实现仓库管理系统的概要设计和部分代码示例。你可以根据具体需求进一步完善和优化。
阅读全文