idea 用JFrame创建页面,写出商品信息能够链接数据库的,商品购物功能的代码
时间: 2024-02-29 11:51:17 浏览: 52
好的,以下是使用 Java Swing 编写能够链接数据库的商品购物功能的示例代码,包含商品信息的显示:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class MainFrame extends JFrame implements ActionListener {
private JPanel panel1, panel2;
private JLabel label1, label2, label3;
private JTextField textField1, textField2, textField3;
private JButton button1, button2;
private JTable table;
private DefaultTableModel model;
private Connection connection;
private Statement statement;
public MainFrame() {
super("商品购物");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 400);
panel1 = new JPanel();
panel1.setLayout(new GridLayout(4, 2));
label1 = new JLabel("商品编号:");
textField1 = new JTextField();
label2 = new JLabel("商品名称:");
textField2 = new JTextField();
label3 = new JLabel("商品单价:");
textField3 = new JTextField();
button1 = new JButton("购买");
button1.addActionListener(this);
button2 = new JButton("清空");
button2.addActionListener(this);
panel1.add(label1);
panel1.add(textField1);
panel1.add(label2);
panel1.add(textField2);
panel1.add(label3);
panel1.add(textField3);
panel1.add(button1);
panel1.add(button2);
add(panel1, BorderLayout.NORTH);
panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
model = new DefaultTableModel();
model.addColumn("编号");
model.addColumn("名称");
model.addColumn("单价");
model.addColumn("库存");
model.addColumn("销量");
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
panel2.add(scrollPane);
add(panel2, BorderLayout.CENTER);
try {
// 连接数据库
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "123456");
statement = connection.createStatement();
// 查询商品信息
ResultSet resultSet = statement.executeQuery("SELECT * FROM goods");
while (resultSet.next()) {
String id = resultSet.getString("id");
String name = resultSet.getString("name");
String price = resultSet.getString("price");
int stock = resultSet.getInt("stock");
int sales = resultSet.getInt("sales");
model.addRow(new Object[]{id, name, price, stock, sales});
}
} catch (Exception e) {
e.printStackTrace();
}
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
// 购买商品
String id = textField1.getText();
String name = textField2.getText();
String price = textField3.getText();
try {
// 查询是否存在该商品
ResultSet resultSet = statement.executeQuery("SELECT * FROM goods WHERE id=" + id);
if (resultSet.next()) {
// 更新商品库存和销量
int stock = resultSet.getInt("stock");
int sales = resultSet.getInt("sales");
if (stock > 0) {
statement.executeUpdate("UPDATE goods SET stock=" + (stock - 1) + ", sales=" + (sales + 1) + " WHERE id=" + id);
JOptionPane.showMessageDialog(panel1, "购买成功!");
// 更新表格中的数据
model.setRowCount(0);
ResultSet resultSet2 = statement.executeQuery("SELECT * FROM goods");
while (resultSet2.next()) {
String id2 = resultSet2.getString("id");
String name2 = resultSet2.getString("name");
String price2 = resultSet2.getString("price");
int stock2 = resultSet2.getInt("stock");
int sales2 = resultSet2.getInt("sales");
model.addRow(new Object[]{id2, name2, price2, stock2, sales2});
}
} else {
JOptionPane.showMessageDialog(panel1, "库存不足!");
}
} else {
JOptionPane.showMessageDialog(panel1, "该商品不存在!");
}
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == button2) {
// 清空输入框内容
textField1.setText("");
textField2.setText("");
textField3.setText("");
}
}
public static void main(String[] args) {
new MainFrame();
}
}
```
这个示例代码使用了 MySQL 数据库,数据库名为 `shop`,用户名为 `root`,密码为 `123456`。购买商品时,会查询数据库中是否存在该商品,如果存在则更新商品库存和销量。购买成功后会弹出提示框,更新表格中的数据。
阅读全文