用java写花店管理系统sql数据库
时间: 2023-11-06 07:08:28 浏览: 96
当你使用Java编写花店管理系统时,你可以选择使用MySQL或者其他SQL数据库。以下是一个示例的SQL数据库模型,你可以在此基础上进行修改和优化:
1. 商品表(product)
- id (int, primary key)
- name (varchar)
- price (decimal)
- description (text)
2. 订单表(order)
- id (int, primary key)
- customer_name (varchar)
- customer_email (varchar)
- total_price (decimal)
- order_date (timestamp)
3. 订单明细表(order_detail)
- id (int, primary key)
- order_id (int, foreign key)
- product_id (int, foreign key)
- quantity (int)
- price (decimal)
4. 客户表(customer)
- id (int, primary key)
- name (varchar)
- email (varchar)
- phone (varchar)
- address (text)
你可以根据你的需求进行修改和优化,例如添加供应商表、员工表等等。同时,你需要使用Java JDBC API来连接和操作数据库。
相关问题
用java写一个有图形界面的花店管理系统
好的,下面是一个简单的Java花店管理系统的实现步骤:
1. 创建Java项目并添加Swing库。同样可以通过Idea中的“File”->“New”->“Project”来创建一个新的Java项目,并在项目的“External Libraries”中添加Swing库。
2. 创建主界面。可以通过创建一个新的JFrame类来实现。可以在Idea的“Project”窗口中右键单击项目,选择“New”->“JFrame Class”来创建一个新的JFrame类。然后在JFrame类中添加需要的组件,例如菜单栏、工具栏、按钮、文本框等等。以下是一个简单的示例代码:
```java
import javax.swing.*;
import java.awt.*;
public class FlowerShopGUI extends JFrame {
private JTextField nameTextField;
private JTextField addressTextField;
private JTextField phoneTextField;
private JButton addButton;
private JButton searchButton;
private JTable flowerTable;
public FlowerShopGUI() {
setTitle("Flower Shop Management System");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add components to the frame
JPanel inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(new JLabel("Name:"));
nameTextField = new JTextField();
inputPanel.add(nameTextField);
inputPanel.add(new JLabel("Address:"));
addressTextField = new JTextField();
inputPanel.add(addressTextField);
inputPanel.add(new JLabel("Phone:"));
phoneTextField = new JTextField();
inputPanel.add(phoneTextField);
addButton = new JButton("Add");
searchButton = new JButton("Search");
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(addButton);
buttonPanel.add(searchButton);
flowerTable = new JTable();
setLayout(new BorderLayout());
add(inputPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
add(new JScrollPane(flowerTable), BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new FlowerShopGUI();
}
}
```
3. 创建数据模型。可以创建一个FlowerShop类来存储花店的基本信息,例如名称、地址、电话号码等等。可以在FlowerShop类中添加相应的属性和方法来实现这一功能。以下是一个简单的示例代码:
```java
public class FlowerShop {
private String name;
private String address;
private String phone;
public FlowerShop(String name, String address, String phone) {
this.name = name;
this.address = address;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
```
4. 连接数据库。可以使用MySQL或者其他的数据库软件来创建一个新的数据库。可以使用JDBC来连接数据库并进行数据的读取和写入。以下是一个简单的示例代码:
```java
import java.sql.*;
public class FlowerShopDAO {
private static final String DB_URL = "jdbc:mysql://localhost:3306/flower_shop";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
public static void addFlowerShop(FlowerShop flowerShop) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
String sql = "INSERT INTO flower_shops (name, address, phone) VALUES (?, ?, ?)";
statement = connection.prepareStatement(sql);
statement.setString(1, flowerShop.getName());
statement.setString(2, flowerShop.getAddress());
statement.setString(3, flowerShop.getPhone());
statement.executeUpdate();
} finally {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
}
public static List<FlowerShop> searchFlowerShop(String keyword) throws SQLException {
List<FlowerShop> flowerShops = new ArrayList<>();
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
String sql = "SELECT * FROM flower_shops WHERE name LIKE ?";
statement = connection.prepareStatement(sql);
statement.setString(1, "%" + keyword + "%");
resultSet = statement.executeQuery();
while (resultSet.next()) {
String name = resultSet.getString("name");
String address = resultSet.getString("address");
String phone = resultSet.getString("phone");
FlowerShop flowerShop = new FlowerShop(name, address, phone);
flowerShops.add(flowerShop);
}
} finally {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
return flowerShops;
}
}
```
5. 将主界面和数据模型连接起来。可以在主界面中添加相应的事件处理程序来实现对数据模型的读写。例如,当用户点击“添加花卉”按钮时,可以弹出一个对话框来让用户输入花卉的信息,然后将这些信息保存到数据库中。当用户点击“查询花卉”按钮时,可以从数据库中读取花卉的信息,并在主界面中显示出来。以下是一个简单的示例代码:
```java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;
public class FlowerShopGUI extends JFrame {
private JTextField nameTextField;
private JTextField addressTextField;
private JTextField phoneTextField;
private JButton addButton;
private JButton searchButton;
private JTable flowerTable;
public FlowerShopGUI() {
setTitle("Flower Shop Management System");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add components to the frame
JPanel inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(new JLabel("Name:"));
nameTextField = new JTextField();
inputPanel.add(nameTextField);
inputPanel.add(new JLabel("Address:"));
addressTextField = new JTextField();
inputPanel.add(addressTextField);
inputPanel.add(new JLabel("Phone:"));
phoneTextField = new JTextField();
inputPanel.add(phoneTextField);
addButton = new JButton("Add");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameTextField.getText();
String address = addressTextField.getText();
String phone = phoneTextField.getText();
FlowerShop flowerShop = new FlowerShop(name, address, phone);
try {
FlowerShopDAO.addFlowerShop(flowerShop);
JOptionPane.showMessageDialog(FlowerShopGUI.this, "Flower shop added successfully!");
} catch (SQLException ex) {
JOptionPane.showMessageDialog(FlowerShopGUI.this, "Failed to add flower shop: " + ex.getMessage());
}
}
});
searchButton = new JButton("Search");
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String keyword = JOptionPane.showInputDialog(FlowerShopGUI.this, "Enter keyword:");
if (keyword != null && !keyword.isEmpty()) {
try {
List<FlowerShop> flowerShops = FlowerShopDAO.searchFlowerShop(keyword);
FlowerShopTableModel model = new FlowerShopTableModel(flowerShops);
flowerTable.setModel(model);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(FlowerShopGUI.this, "Failed to search flower shops: " + ex.getMessage());
}
}
}
});
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(addButton);
buttonPanel.add(searchButton);
flowerTable = new JTable();
setLayout(new BorderLayout());
add(inputPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.SOUTH);
add(new JScrollPane(flowerTable), BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new FlowerShopGUI();
}
}
```
至此,一个简单的Java花店管理系统就完成了。当然,这只是一个基础的框架,你还需要根据具体的需求来完善它。
阅读全文