用java做一个增删查改的花店管理系统代码实现
时间: 2023-08-27 20:04:09 浏览: 54
好的,以下是一个使用Java语言实现的增删查改的花店管理系统的代码,其中使用了Java Swing作为UI框架和MySQL作为数据库:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class FlowerShopManagementSystem extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel titleLabel, nameLabel, priceLabel, colorLabel, flowerMeaningLabel, IDLabel;
private JTextField nameField, priceField, colorField, flowerMeaningField, IDField;
private JButton addButton, deleteButton, updateButton, searchButton;
private JPanel panel;
private Connection connection;
private Statement statement;
public FlowerShopManagementSystem() {
this.setTitle("花店管理系统");
this.setSize(500, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
titleLabel = new JLabel("花店管理系统");
titleLabel.setFont(new Font("宋体", Font.BOLD, 20));
nameLabel = new JLabel("花卉名称:");
priceLabel = new JLabel("花卉价格:");
colorLabel = new JLabel("花卉颜色:");
flowerMeaningLabel = new JLabel("花卉花语:");
IDLabel = new JLabel("花卉ID:");
nameField = new JTextField(10);
priceField = new JTextField(10);
colorField = new JTextField(10);
flowerMeaningField = new JTextField(10);
IDField = new JTextField(10);
addButton = new JButton("添加");
deleteButton = new JButton("删除");
updateButton = new JButton("修改");
searchButton = new JButton("查询");
addButton.addActionListener(this);
deleteButton.addActionListener(this);
updateButton.addActionListener(this);
searchButton.addActionListener(this);
panel = new JPanel();
panel.setLayout(new GridLayout(6, 2));
panel.add(IDLabel);
panel.add(IDField);
panel.add(nameLabel);
panel.add(nameField);
panel.add(priceLabel);
panel.add(priceField);
panel.add(colorLabel);
panel.add(colorField);
panel.add(flowerMeaningLabel);
panel.add(flowerMeaningField);
panel.add(addButton);
panel.add(deleteButton);
panel.add(updateButton);
panel.add(searchButton);
this.add(titleLabel, BorderLayout.NORTH);
this.add(panel, BorderLayout.CENTER);
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/flower_shop", "root", "your_password");
statement = connection.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
try {
String query = "insert into flower (flower_id, flower_name, flower_price, flower_color, flower_meaning) values ('"
+ IDField.getText() + "', '" + nameField.getText() + "', '" + priceField.getText() + "', '"
+ colorField.getText() + "', '" + flowerMeaningField.getText() + "')";
statement.executeUpdate(query);
JOptionPane.showMessageDialog(null, "花卉添加成功!");
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == deleteButton) {
try {
String query = "delete from flower where flower_id='" + IDField.getText() + "' or flower_name='"
+ nameField.getText() + "'";
statement.executeUpdate(query);
JOptionPane.showMessageDialog(null, "花卉删除成功!");
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == updateButton) {
try {
String query = "update flower set flower_name='" + nameField.getText() + "', flower_price='"
+ priceField.getText() + "', flower_color='" + colorField.getText() + "', flower_meaning='"
+ flowerMeaningField.getText() + "' where flower_id='" + IDField.getText() + "'";
statement.executeUpdate(query);
JOptionPane.showMessageDialog(null, "花卉修改成功!");
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == searchButton) {
try {
String query = "select * from flower where flower_id='" + IDField.getText() + "' or flower_name='"
+ nameField.getText() + "'";
ResultSet resultSet = statement.executeQuery(query);
if (resultSet.next()) {
nameField.setText(resultSet.getString("flower_name"));
priceField.setText(resultSet.getString("flower_price"));
colorField.setText(resultSet.getString("flower_color"));
flowerMeaningField.setText(resultSet.getString("flower_meaning"));
} else {
JOptionPane.showMessageDialog(null, "未找到对应花卉!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new FlowerShopManagementSystem();
}
}
```
以上代码实现了增删查改的功能,你可以根据自己的需要进行修改和扩展。注意要替换掉代码中的数据库连接信息,并创建`flower_shop`数据库和`flower`表。