用IDEA编写一个java GUI 订单系统,一个订单中有多个商品,商品包含商品名、商品价格等。编写 程序实现订单的增加和查询,并能够按照订单的销售金额进行排序,连接数据库
时间: 2024-03-09 07:44:16 浏览: 73
好的,我可以帮你解答这个问题。首先,我们需要明确一些前置知识点:Java GUI 编程、MySQL 数据库连接、SQL 语句的编写等等。
Java GUI 编程可以使用 Swing 或 JavaFX 等框架,这里我选择使用 Swing。订单系统需要实现订单的增加和查询,并能够按照订单的销售金额进行排序,因此我们需要设计一个 Order 类和一个 OrderSystem 类。
Order 类可以包含以下属性:
- 订单编号(orderID)
- 商品列表(itemList)
- 订单总价(totalPrice)
Item 类可以包含以下属性:
- 商品名称(itemName)
- 商品价格(price)
OrderSystem 类可以包含以下方法:
- addOrder(Order order):增加一个订单
- searchOrder(String orderID):查询订单
- sortByPrice():按照订单总价进行排序
在连接 MySQL 数据库方面,我们可以使用 JDBC API 来实现。具体步骤如下:
1. 加载 MySQL JDBC 驱动程序
```java
Class.forName("com.mysql.jdbc.Driver");
```
2. 建立数据库连接
```java
Connection conn = DriverManager.getConnection(url, username, password);
```
其中,url、username 和 password 分别为数据库连接字符串、用户名和密码。
3. 创建 Statement 对象,并执行 SQL 语句
```java
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
```
其中,sql 为 SQL 语句。
4. 处理查询结果
```java
while (rs.next()) {
// 处理每一行记录
}
```
接下来我们来实现这个订单系统。首先是 Order 类:
```java
import java.util.ArrayList;
import java.util.List;
public class Order {
private String orderID; // 订单编号
private List<Item> itemList; // 商品列表
private double totalPrice; // 订单总价
public Order(String orderID) {
this.orderID = orderID;
this.itemList = new ArrayList<>();
this.totalPrice = 0;
}
public void addItem(Item item) {
itemList.add(item);
totalPrice += item.getPrice();
}
public String getOrderID() {
return orderID;
}
public List<Item> getItemList() {
return itemList;
}
public double getTotalPrice() {
return totalPrice;
}
}
```
然后是 Item 类:
```java
public class Item {
private String itemName; // 商品名称
private double price; // 商品价格
public Item(String itemName, double price) {
this.itemName = itemName;
this.price = price;
}
public String getItemName() {
return itemName;
}
public double getPrice() {
return price;
}
}
```
接下来是 OrderSystem 类:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class OrderSystem extends JFrame implements ActionListener {
private JTextField orderIDField;
private JTextField itemNameField;
private JTextField priceField;
private JTextArea orderListArea;
private JButton addOrderButton;
private JButton searchOrderButton;
private JButton sortByPriceButton;
private Connection conn;
public OrderSystem() {
// 初始化数据库连接
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/order_system";
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
// 初始化界面
setTitle("订单系统");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JLabel orderIDLabel = new JLabel("订单编号:");
orderIDField = new JTextField(20);
add(orderIDLabel);
add(orderIDField);
JLabel itemNameLabel = new JLabel("商品名称:");
itemNameField = new JTextField(20);
add(itemNameLabel);
add(itemNameField);
JLabel priceLabel = new JLabel("商品价格:");
priceField = new JTextField(20);
add(priceLabel);
add(priceField);
addOrderButton = new JButton("增加订单");
addOrderButton.addActionListener(this);
add(addOrderButton);
searchOrderButton = new JButton("查询订单");
searchOrderButton.addActionListener(this);
add(searchOrderButton);
sortByPriceButton = new JButton("按销售金额排序");
sortByPriceButton.addActionListener(this);
add(sortByPriceButton);
orderListArea = new JTextArea(10, 30);
orderListArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(orderListArea);
add(scrollPane);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addOrderButton) {
String orderID = orderIDField.getText().trim();
String itemName = itemNameField.getText().trim();
double price = Double.parseDouble(priceField.getText().trim());
Item item = new Item(itemName, price);
try {
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO orders VALUES (?, ?, ?)");
pstmt.setString(1, orderID);
pstmt.setString(2, itemName);
pstmt.setDouble(3, price);
pstmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM orders WHERE orderID=?");
pstmt.setString(1, orderID);
ResultSet rs = pstmt.executeQuery();
Order order = new Order(orderID);
while (rs.next()) {
String itemName1 = rs.getString("itemName");
double price1 = rs.getDouble("price");
Item item1 = new Item(itemName1, price1);
order.addItem(item1);
}
orderListArea.append("订单 " + order.getOrderID() + " 总价:" + order.getTotalPrice() + "\n");
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == searchOrderButton) {
String orderID = orderIDField.getText().trim();
try {
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM orders WHERE orderID=?");
pstmt.setString(1, orderID);
ResultSet rs = pstmt.executeQuery();
Order order = new Order(orderID);
while (rs.next()) {
String itemName = rs.getString("itemName");
double price = rs.getDouble("price");
Item item = new Item(itemName, price);
order.addItem(item);
}
orderListArea.append("订单 " + order.getOrderID() + " 总价:" + order.getTotalPrice() + "\n");
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == sortByPriceButton) {
orderListArea.setText("");
try {
Statement stmt = conn.createStatement();
String sql = "SELECT orderID, SUM(price) AS total_price FROM orders GROUP BY orderID ORDER BY total_price DESC";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String orderID = rs.getString("orderID");
double totalPrice = rs.getDouble("total_price");
orderListArea.append("订单 " + orderID + " 总价:" + totalPrice + "\n");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new OrderSystem();
}
}
```
这个订单系统实现了增加订单、查询订单和按销售金额排序三个功能。在增加订单和查询订单时,会将订单数据保存到 MySQL 数据库中;在按销售金额排序时,会从 MySQL 数据库中读取订单数据并按照销售金额排序。注意,在使用 PreparedStatement 执行 SQL 语句时,要使用占位符(?)来避免 SQL 注入攻击。
阅读全文