做一个java与mysql的仓库管理系统
时间: 2024-03-10 14:48:44 浏览: 33
好的,以下是一个简单的Java与MySQL的仓库管理系统的代码示例:
```
import java.sql.*;
import java.util.Scanner;
public class WarehouseSystem {
private Connection conn;
public WarehouseSystem() {
try {
String url = "jdbc:mysql://localhost:3306/mydatabase"; // 数据库连接地址
String user = "root"; // 数据库用户名
String password = "password"; // 数据库密码
conn = DriverManager.getConnection(url, user, password); // 建立数据库连接
System.out.println("Database connection established");
} catch (SQLException e) {
System.err.println("Database connection error: " + e.getMessage());
}
}
public void run() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:1.添加物品 2.删除物品 3.打印物品列表 4.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addItem();
break;
case 2:
removeItem();
break;
case 3:
printItems();
break;
case 4:
System.out.println("退出程序");
return;
default:
System.out.println("无效操作,请重新选择");
}
}
}
private void addItem() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入物品名称:");
String name = scanner.nextLine();
System.out.println("请输入物品数量:");
int quantity = scanner.nextInt();
try {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO items (name, quantity) VALUES (?, ?)");
stmt.setString(1, name);
stmt.setInt(2, quantity);
stmt.executeUpdate();
System.out.println("物品已添加");
} catch (SQLException e) {
System.err.println("Database error: " + e.getMessage());
}
}
private void removeItem() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的物品ID:");
int id = scanner.nextInt();
try {
PreparedStatement stmt = conn.prepareStatement("DELETE FROM items WHERE id = ?");
stmt.setInt(1, id);
int rows = stmt.executeUpdate();
if (rows > 0) {
System.out.println("物品已删除");
} else {
System.out.println("找不到指定的物品");
}
} catch (SQLException e) {
System.err.println("Database error: " + e.getMessage());
}
}
private void printItems() {
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM items");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int quantity = rs.getInt("quantity");
System.out.println(id + ": " + name + " (quantity: " + quantity + ")");
}
} catch (SQLException e) {
System.err.println("Database error: " + e.getMessage());
}
}
public void close() {
try {
conn.close(); // 关闭数据库连接
} catch (SQLException e) {
System.err.println("Database error: " + e.getMessage());
}
}
public static void main(String[] args) {
WarehouseSystem system = new WarehouseSystem();
system.run();
system.close();
}
}
```
这个代码示例实现了一个简单的仓库管理系统,使用了Java与MySQL数据库进行交互。系统包括三个方法:添加物品、删除物品和打印物品列表。在`main`方法中,首先创建一个`WarehouseSystem`对象,并调用`run`方法来运行系统。调用`close`方法来关闭数据库连接。
在`addItem`方法中,系统要求用户输入物品名称和数量,并使用`PreparedStatement`对象来将这些数据插入到`items`表中。`PreparedStatement`对象使用了占位符来避免SQL注入攻击。
在`removeItem`方法中,系统要求用户输入要删除的物品ID,并使用`PreparedStatement`对象来从`items`表中删除指定的物品。
在`printItems`方法中,系统查询`items`表中的所有物品,并使用`ResultSet`对象遍历查询结果,打印每个物品的ID、名称和数量。
当然,这只是一个简单的示例,实际应用中可能需要更多的功能和优化。例如,可能需要支持搜索物品、修改物品信息等操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)