使用Java写一段代码,要求写出衣服管理系统
时间: 2024-01-11 14:02:44 浏览: 25
好的,以下是一个简单的衣服管理系统的Java代码示例,包括添加、删除、修改和查询衣服信息的功能:
```java
import java.sql.*;
import java.util.Scanner;
public class ClothesManager {
private Connection conn;
private Statement stmt;
private Scanner scanner;
public ClothesManager() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/clothes_db";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
scanner = new Scanner(System.in);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void addClothes() {
System.out.println("请输入衣服信息:");
System.out.print("名称:");
String name = scanner.nextLine();
System.out.print("尺寸:");
String size = scanner.nextLine();
System.out.print("颜色:");
String color = scanner.nextLine();
System.out.print("材质:");
String material = scanner.nextLine();
System.out.print("单价:");
double price = scanner.nextDouble();
scanner.nextLine();
try {
String sql = "INSERT INTO clothes (name, size, color, material, price) VALUES ('" +
name + "', '" + size + "', '" +
color + "', '" + material + "', " +
price + ")";
stmt.executeUpdate(sql);
System.out.println("添加成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteClothes() {
System.out.print("请输入要删除的衣服ID:");
int id = scanner.nextInt();
scanner.nextLine();
try {
String sql = "DELETE FROM clothes WHERE id = " + id;
int result = stmt.executeUpdate(sql);
if (result == 1) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败,未找到对应的衣服ID!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateClothes() {
System.out.print("请输入要修改的衣服ID:");
int id = scanner.nextInt();
scanner.nextLine();
try {
String sql = "SELECT * FROM clothes WHERE id = " + id;
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
System.out.println("当前衣服信息为:");
System.out.println("ID:" + rs.getInt("id"));
System.out.println("名称:" + rs.getString("name"));
System.out.println("尺寸:" + rs.getString("size"));
System.out.println("颜色:" + rs.getString("color"));
System.out.println("材质:" + rs.getString("material"));
System.out.println("单价:" + rs.getDouble("price"));
System.out.println("请输入新的衣服信息:");
System.out.print("名称:");
String name = scanner.nextLine();
System.out.print("尺寸:");
String size = scanner.nextLine();
System.out.print("颜色:");
String color = scanner.nextLine();
System.out.print("材质:");
String material = scanner.nextLine();
System.out.print("单价:");
double price = scanner.nextDouble();
scanner.nextLine();
sql = "UPDATE clothes SET name = '" + name + "', size = '" + size + "', " +
"color = '" + color + "', material = '" + material + "', price = " + price +
" WHERE id = " + id;
stmt.executeUpdate(sql);
System.out.println("修改成功!");
} else {
System.out.println("未找到对应的衣服ID!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void queryClothes() {
try {
String sql = "SELECT * FROM clothes";
ResultSet rs = stmt.executeQuery(sql);
System.out.println("衣服信息列表:");
while (rs.next()) {
System.out.println("ID:" + rs.getInt("id"));
System.out.println("名称:" + rs.getString("name"));
System.out.println("尺寸:" + rs.getString("size"));
System.out.println("颜色:" + rs.getString("color"));
System.out.println("材质:" + rs.getString("material"));
System.out.println("单价:" + rs.getDouble("price"));
System.out.println("---------------------------");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void close() {
try {
stmt.close();
conn.close();
scanner.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ClothesManager manager = new ClothesManager();
while (true) {
System.out.println("请选择要执行的操作:");
System.out.println("1. 添加衣服");
System.out.println("2. 删除衣服");
System.out.println("3. 修改衣服");
System.out.println("4. 查询衣服");
System.out.println("5. 退出程序");
int choice = manager.scanner.nextInt();
manager.scanner.nextLine();
switch (choice) {
case 1:
manager.addClothes();
break;
case 2:
manager.deleteClothes();
break;
case 3:
manager.updateClothes();
break;
case 4:
manager.queryClothes();
break;
case 5:
manager.close();
System.exit(0);
default:
System.out.println("输入错误,请重新选择操作!");
}
}
}
}
```
以上代码使用了MySQL数据库,并通过控制台实现了用户交互界面。您可以根据需要进行修改和优化。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)