用java编写一个简单的使用数据库储存信息的食堂管理系统
时间: 2023-08-22 08:06:36 浏览: 111
好的,下面是一个使用Java语言和MySQL数据库的简单食堂管理系统的示例代码:
1. 首先需要创建MySQL数据库和相应的数据表,例如创建一个名为canteen的数据库,其中有一个名为dishes的数据表,包含字段id、name、price和inventory,用于存储菜品的信息。可以使用以下SQL语句创建该数据表:
```
CREATE DATABASE canteen;
USE canteen;
CREATE TABLE dishes (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
price DECIMAL(8,2) NOT NULL,
inventory INT NOT NULL
);
```
2. 在Java程序中使用JDBC连接MySQL数据库,并实现菜品信息的增删改查功能。可以使用以下代码作为示例:
```
import java.sql.*;
public class CanteenManagementSystem {
// 数据库连接信息
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/canteen?useSSL=false&serverTimezone=UTC";
private static final String JDBC_USERNAME = "root";
private static final String JDBC_PASSWORD = "password";
// 菜品信息增删改查的SQL语句
private static final String INSERT_DISH = "INSERT INTO dishes(name, price, inventory) VALUES (?, ?, ?)";
private static final String DELETE_DISH = "DELETE FROM dishes WHERE id = ?";
private static final String UPDATE_DISH = "UPDATE dishes SET name = ?, price = ?, inventory = ? WHERE id = ?";
private static final String SELECT_DISH = "SELECT * FROM dishes WHERE id = ?";
public static void main(String[] args) {
// 建立数据库连接
try (Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD)) {
// 添加一道新菜
addDish(conn, "红烧肉", 28.0, 50);
// 修改一道菜的信息
updateDish(conn, 1, "红烧排骨", 32.0, 30);
// 查询一道菜的信息
Dish dish = selectDish(conn, 1);
System.out.println(dish);
// 删除一道菜
deleteDish(conn, 1);
} catch (SQLException e) {
e.printStackTrace();
}
}
// 添加一道新菜
private static void addDish(Connection conn, String name, double price, int inventory) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(INSERT_DISH, Statement.RETURN_GENERATED_KEYS);
stmt.setString(1, name);
stmt.setDouble(2, price);
stmt.setInt(3, inventory);
stmt.executeUpdate();
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) {
int id = rs.getInt(1);
System.out.println("新增菜品成功,ID为" + id);
}
rs.close();
stmt.close();
}
// 修改一道菜的信息
private static void updateDish(Connection conn, int id, String name, double price, int inventory) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(UPDATE_DISH);
stmt.setString(1, name);
stmt.setDouble(2, price);
stmt.setInt(3, inventory);
stmt.setInt(4, id);
int rows = stmt.executeUpdate();
if (rows > 0) {
System.out.println("修改菜品信息成功");
}
stmt.close();
}
// 查询一道菜的信息
private static Dish selectDish(Connection conn, int id) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(SELECT_DISH);
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
Dish dish = null;
if (rs.next()) {
String name = rs.getString("name");
double price = rs.getDouble("price");
int inventory = rs.getInt("inventory");
dish = new Dish(id, name, price, inventory);
}
rs.close();
stmt.close();
return dish;
}
// 删除一道菜
private static void deleteDish(Connection conn, int id) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(DELETE_DISH);
stmt.setInt(1, id);
int rows = stmt.executeUpdate();
if (rows > 0) {
System.out.println("删除菜品成功");
}
stmt.close();
}
// 菜品信息类
private static class Dish {
private int id;
private String name;
private double price;
private int inventory;
public Dish(int id, String name, double price, int inventory) {
this.id = id;
this.name = name;
this.price = price;
this.inventory = inventory;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getInventory() {
return inventory;
}
@Override
public String toString() {
return "菜品信息:ID=" + id + ", 名称=" + name + ", 价格=" + price + ", 库存=" + inventory;
}
}
}
```
以上代码实现了食堂管理系统中的菜品信息的增删改查功能。您可以根据需要进行修改和扩展。
阅读全文