给出代码 菜品管理模块:使用Java的GUI类实现菜品信息的展示及增删改查操作。使用JDBC与数据库进行交互
时间: 2024-03-12 16:47:54 浏览: 80
好的,我来给你提供一份Java的GUI类实现菜品信息的展示及增删改查操作的代码,使用JDBC与数据库进行交互。
首先,我们需要创建一个菜品管理的窗口,可以使用Swing库实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DishManagement extends JFrame implements ActionListener {
private JLabel nameLabel, priceLabel;
private JTextField nameText, priceText;
private JButton addButton, deleteButton, updateButton, searchButton;
private JTable table;
public DishManagement() {
setTitle("菜品管理");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
nameLabel = new JLabel("菜名");
nameText = new JTextField(10);
priceLabel = new JLabel("价格");
priceText = new JTextField(10);
addButton = new JButton("添加");
deleteButton = new JButton("删除");
updateButton = new JButton("更新");
searchButton = new JButton("查询");
JPanel panel = new JPanel(new FlowLayout());
panel.add(nameLabel);
panel.add(nameText);
panel.add(priceLabel);
panel.add(priceText);
panel.add(addButton);
panel.add(deleteButton);
panel.add(updateButton);
panel.add(searchButton);
table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
add(panel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
addButton.addActionListener(this);
deleteButton.addActionListener(this);
updateButton.addActionListener(this);
searchButton.addActionListener(this);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO: 实现增删改查操作
}
}
```
接下来,我们需要使用JDBC与数据库进行交互,实现菜品信息的增删改查操作。我们可以使用MySQL数据库,并使用JDBC连接MySQL数据库:
```java
import java.sql.*;
public class DishDatabase {
private Connection connection;
private Statement statement;
public DishDatabase() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/dish_management?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "123456";
connection = DriverManager.getConnection(url, user, password);
statement = connection.createStatement();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public ResultSet executeQuery(String sql) {
ResultSet resultSet = null;
try {
resultSet = statement.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return resultSet;
}
public int executeUpdate(String sql) {
int result = 0;
try {
result = statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public void close() {
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
现在,我们可以在`actionPerformed`方法中实现菜品信息的增删改查操作了:
```java
@Override
public void actionPerformed(ActionEvent e) {
DishDatabase database = new DishDatabase();
String name = nameText.getText();
String price = priceText.getText();
String sql;
switch (e.getActionCommand()) {
case "添加":
sql = "insert into dish (name, price) values ('" + name + "', " + price + ")";
database.executeUpdate(sql);
break;
case "删除":
sql = "delete from dish where name = '" + name + "'";
database.executeUpdate(sql);
break;
case "更新":
sql = "update dish set price = " + price + " where name = '" + name + "'";
database.executeUpdate(sql);
break;
case "查询":
sql = "select * from dish where name like '%" + name + "%'";
ResultSet resultSet = database.executeQuery(sql);
table.setModel(DbUtils.resultSetToTableModel(resultSet));
break;
}
database.close();
}
```
最后,我们需要在MySQL数据库中创建一个`dish`表,用于存储菜品信息:
```sql
CREATE TABLE `dish` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`price` double NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
现在,我们就完成了菜品管理模块的代码实现。
阅读全文