用idea,使用JFrame创建页面,商品信息能链接数据库的,有对应购物功能的代码
时间: 2024-02-29 07:57:21 浏览: 130
好的,以下是一个简单的JFrame页面,包含了商品信息的链接数据库以及购物功能的代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class ShoppingPage extends JFrame {
private JPanel mainPanel;
private JTable productTable;
private JButton addToCartButton;
// 数据库连接信息
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/shopping_db";
private static final String USER = "root";
private static final String PASSWORD = "password";
// 商品信息表格
private static final String[] COLUMN_NAMES = {"ID", "Name", "Price", "Stock"};
private static final Object[][] DATA = {};
// 购物车信息表格
private static final String[] CART_COLUMN_NAMES = {"Name", "Price", "Quantity", "Subtotal"};
private static final Object[][] CART_DATA = {};
private DefaultTableModel productTableModel;
private DefaultTableModel cartTableModel;
public ShoppingPage() {
setTitle("Shopping Page");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(800, 600));
setContentPane(mainPanel);
pack();
// 初始化商品信息表格
productTableModel = new DefaultTableModel(DATA, COLUMN_NAMES);
productTable.setModel(productTableModel);
// 初始化购物车信息表格
cartTableModel = new DefaultTableModel(CART_DATA, CART_COLUMN_NAMES);
// 添加按钮点击事件
addToCartButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取选中商品的名称、价格、库存
int row = productTable.getSelectedRow();
String name = (String) productTableModel.getValueAt(row, 1);
double price = (double) productTableModel.getValueAt(row, 2);
int stock = (int) productTableModel.getValueAt(row, 3);
// 弹出输入框,让用户输入购买数量
String input = JOptionPane.showInputDialog("Enter quantity (1-" + stock + "):");
if (input == null) {
// 用户取消输入
return;
}
int quantity = Integer.parseInt(input);
if (quantity < 1 || quantity > stock) {
// 数量不在允许范围内
JOptionPane.showMessageDialog(null, "Invalid quantity!");
return;
}
// 计算小计金额,并将购物车信息添加到表格中
double subtotal = price * quantity;
Object[] cartRow = {name, price, quantity, subtotal};
cartTableModel.addRow(cartRow);
}
});
// 从数据库中加载商品信息
loadProductsFromDatabase();
}
private void loadProductsFromDatabase() {
Connection conn = null;
Statement stmt = null;
try {
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
// 打开连接
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
// 执行查询
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql = "SELECT * FROM products";
ResultSet rs = stmt.executeQuery(sql);
// 处理结果集,将商品信息添加到表格中
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
double price = rs.getDouble("price");
int stock = rs.getInt("stock");
Object[] row = {id, name, price, stock};
productTableModel.addRow(row);
}
// 关闭资源
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// 处理 JDBC 错误
se.printStackTrace();
} catch (Exception e) {
// 处理 Class.forName 错误
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se2) {
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
se.printStackTrace();
}
}
}
public static void main(String[] args) {
ShoppingPage shoppingPage = new ShoppingPage();
shoppingPage.setVisible(true);
}
}
```
请将代码中的数据库连接信息替换为您的实际信息,并创建一个`products`表,包含`id`、`name`、`price`和`stock`四个字段,用于存储商品信息。
希望这个代码能对您有所帮助!
阅读全文