-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bao.java
145 lines (137 loc) · 6.43 KB
/
Bao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package UI;
import dao.Xiugai;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.sql.*;
public class Bao {
public static void bao() throws ClassNotFoundException, SQLException {
JFrame f = new JFrame("背包");
f.setSize(800, 600);
f.setLocationRelativeTo(null); // 屏幕居中显示
try {
Image backgroundImage = ImageIO.read(new File("src/images/背包.png"));
JLabel backgroundLabel = new JLabel(new ImageIcon(backgroundImage));
// 创建按钮面板
JPanel bt = new JPanel(new GridLayout(2, 2, 10, 10));
JButton b1 = new JButton("增加(金手指)");
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
dao.Add.add();
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}});
JButton b2 = new JButton("删除(金手指)");
JButton b3 = new JButton("修改(金手指)");
JButton b4 = new JButton("查询(金手指)");
bt.setOpaque(false); // 让面板透明
bt.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // 为面板添加空白边框
bt.add(b1);
bt.add(b2);
bt.add(b3);
bt.add(b4);
// 创建一个面板
JPanel panel = new JPanel(new BorderLayout());
panel.add(bt, BorderLayout.CENTER);
panel.add(backgroundLabel, BorderLayout.WEST);
// 添加表格和退出按钮
Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
String url = "jdbc:mysql://localhost:3306/final?serverTimezone=GMT%2B8&useSSL=false";
String user = "root";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();//创建了一个用于执行 SQL 语句的 Statement 对象。
// 执行查询语句并获取结果集
ResultSet rs = stmt.executeQuery("SELECT * FROM beibao");
ResultSetMetaData metaData = rs.getMetaData();//创建了一个用于执行 SQL 语句的 Statement 对象。
int columnCount = metaData.getColumnCount();
// 构造表格模型,并从结果集中加载数据
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(new String[]{"序号","武器数量", "防具数量", "道具数量", "金币数量"});
// 遍历结果集,并将数据添加到表格模型中
while (rs.next()) {// 循环遍历结果集的所有记录
Object[] rowData = new Object[columnCount];
for (int i = 1; i <= columnCount; ++i) {
rowData[i - 1] = rs.getObject(i);
}
model.addRow(rowData);
}
// 创建 JTable,并将其设置为表格模型
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
// 将表格添加到界面中
f.add(scrollPane, BorderLayout.CENTER);
// 关闭连接和语句
rs.close();
stmt.close();
conn.close();
//删除
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
// 获取选中行的 ID
int ws = (int) table.getValueAt(selectedRow, 0); // 假设第一列是 ID 列
// 构造 DELETE SQL 语句,用选中行的 ID 作为条件
String sql = "DELETE FROM beibao WHERE xuhao = ?";
try (Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement ps = conn.prepareStatement(sql)) {
// 设置参数
ps.setInt(1, ws);
// 执行删除操作
ps.executeUpdate();
// 从表格模型中删除选中行的数据
((DefaultTableModel) table.getModel()).removeRow(selectedRow);
JOptionPane.showMessageDialog(null, "删除成功!");
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败!");
}
} else {
JOptionPane.showMessageDialog(null, "请先选择一行记录!");
}
}
});
//修改
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 创建一个JDialog
try {
new Xiugai();
} catch (ClassNotFoundException | SQLException ex) {
throw new RuntimeException(ex);
}
}});
//查询
b4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dao.Cha.cha();
}
});
JButton exit = new JButton("退出背包");
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.dispose();}});
f.add(exit, BorderLayout.PAGE_END);
// 将面板添加到窗口中
f.add(panel, BorderLayout.NORTH);
} catch (IOException e) {
e.printStackTrace();
}
f.setVisible(true);
}
}