中英翻译字典

初版(已写完基本框架)

一个合格的程序员都是会写注释的,当然这是没有入职之前,入职后就不要写了,不然容易被裁员!还要写几个bug进去,我只能说懂的都懂

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package Endclass.demol2;// 导入必要的包
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

// 定义一个类,继承JFrame,实现ActionListener接口
public class Dictionary extends JFrame implements ActionListener {
// 定义组件
private JLabel label1, label2; // 标签
private JTextField text1, text2; // 文本框
private JButton button1, button2, button3, button4; // 按钮
private JPanel panel1, panel2, panel3; // 面板
private Connection conn; // 数据库连接
private PreparedStatement ps; // 预编译语句
private ResultSet rs; // 结果集

// 定义构造方法
public Dictionary() {
// 设置窗口标题
super("中英翻译字典");
// 设置窗口大小
setSize(400, 300);
// 设置窗口居中
setLocationRelativeTo(null);
// 设置窗口关闭时退出程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口布局为边界布局
setLayout(new BorderLayout());

// 初始化组件
label1 = new JLabel("开始输入:");
label2 = new JLabel("翻译结果:");
text1 = new JTextField(20);
text2 = new JTextField(20);
text2.setEditable(false); // 设置文本框不可编辑

button1 = new JButton("查询");
button2 = new JButton("添加");
button3 = new JButton("修改");
button4 = new JButton("删除");
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();

// 添加组件到面板
panel1.add(label1);
panel1.add(text1);
panel2.add(label2);
panel2.add(text2);
panel3.add(button1);
panel3.add(button2);
panel3.add(button3);
panel3.add(button4);

// 添加面板到窗口
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);

// 设置窗口可见
setVisible(true);

// 注册监听器
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);

// 连接数据库
connectDB();
}

// 定义连接数据库的方法
public void connectDB() {
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dictionary?useSSL=false&serverTimezone=UTC", "root", "123456");
// 输出提示信息
System.out.println("数据库连接成功!");
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

// 定义查询的方法
public void query() {
try {
// 获取输入的单词或汉字
String input = text1.getText().trim();
// 判断输入是否为空
if (input.equals("")) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "请输入单词或汉字!");
// 清空文本框
text1.setText("");
text2.setText("");
// 返回
return;
}
// 定义查询语句
String sql = "select * from words where word = ? or chinese = ?";
// 预编译语句
ps = conn.prepareStatement(sql);
// 设置参数
ps.setString(1, input);
ps.setString(2, input);
// 执行查询
rs = ps.executeQuery();
// 判断结果集是否为空
if (rs.next()) {
// 获取单词和汉字
String word = rs.getString("word");
String chinese = rs.getString("chinese");
// 判断输入是单词还是汉字
if (input.equals(word)) {
// 设置文本框显示汉字
text2.setText(chinese);
} else {
// 设置文本框显示单词
text2.setText(word);
}
} else {
// 弹出提示框
JOptionPane.showMessageDialog(this, "没有找到对应的翻译!");
// 清空文本框
text2.setText("");
}
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

// 定义添加的方法
public void add() {
try {
// 获取输入的单词和汉字
String word = text1.getText().trim();
String chinese = text2.getText().trim();
// 判断输入是否为空
if (word.equals("") || chinese.equals("")) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "请输入单词和汉字!");
// 返回
return;
}
// 定义查询语句
String sql1 = "select * from words where word = ? or chinese = ?";
// 预编译语句
ps = conn.prepareStatement(sql1);
// 设置参数
ps.setString(1, word);
ps.setString(2, chinese);
// 执行查询
rs = ps.executeQuery();
// 判断结果集是否为空
if (rs.next()) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "该单词或汉字已存在!");
// 清空文本框
text1.setText("");
text2.setText("");
// 返回
return;
}
// 定义插入语句
String sql2 = "insert into words values (?, ?)";
// 预编译语句
ps = conn.prepareStatement(sql2);
// 设置参数
ps.setString(1, word);
ps.setString(2, chinese);
// 执行插入
int n = ps.executeUpdate();
// 判断插入是否成功
if (n > 0) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "添加成功!");
// 清空文本框
text1.setText("");
text2.setText("");
} else {
// 弹出提示框
JOptionPane.showMessageDialog(this, "添加失败!");
}
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

// 定义修改的方法
public void update() {
try {
// 获取输入的单词和汉字
String word = text1.getText().trim();
String chinese = text2.getText().trim();
// 判断输入是否为空
if (word.equals("") || chinese.equals("")) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "请输入单词和汉字!");
// 返回
return;
}
// 定义查询语句
String sql1 = "select * from words where word = ? or chinese = ?";
// 预编译语句
ps = conn.prepareStatement(sql1);
// 设置参数
ps.setString(1, word);
ps.setString(2, chinese);
// 执行查询
rs = ps.executeQuery();
// 判断结果集是否为空
if (!rs.next()) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "该单词或汉字不存在!");
// 清空文本框
text1.setText("");
text2.setText("");
// 返回
return;
}
// 定义更新语句
String sql2 = "update words set chinese = ? where word = ?";
// 预编译语句
ps = conn.prepareStatement(sql2);
// 设置参数
ps.setString(1, chinese);
ps.setString(2, word);
// 执行更新
int n = ps.executeUpdate();
// 判断更新是否成功
if (n > 0) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "修改成功!");
// 清空文本框
text1.setText("");
text2.setText("");
} else {
// 弹出提示框
JOptionPane.showMessageDialog(this, "修改失败!");
}
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

// 定义删除的方法
public void delete() {
try {
// 获取输入的单词或汉字
String input = text1.getText().trim();
// 判断输入是否为空
if (input.equals("")) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "请输入单词或汉字!");
// 清空文本框
text1.setText("");
text2.setText("");
// 返回
return;
}
// 定义查询语句
String sql1 = "select * from words where word = ? or chinese = ?";
// 预编译语句
ps = conn.prepareStatement(sql1);
// 设置参数
ps.setString(1, input);
ps.setString(2, input);
// 执行查询
rs = ps.executeQuery();
// 判断结果集是否为空
if (!rs.next()) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "该单词或汉字不存在!");
// 清空文本框
text1.setText("");
text2.setText("");
// 返回
return;
}
// 定义删除语句
String sql2 = "delete from words where word = ? or chinese = ?";
// 预编译语句
ps = conn.prepareStatement(sql2);
// 设置参数
ps.setString(1, input);
ps.setString(2, input);
// 执行删除
int n = ps.executeUpdate();
// 判断删除是否成功
if (n > 0) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "删除成功!");
// 清空文本框
text1.setText("");
text2.setText("");
} else {
// 弹出提示框
JOptionPane.showMessageDialog(this, "删除失败!");
}
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

@Override
// 定义点击事件函数
public void actionPerformed(ActionEvent e) {
// 获取事件源
Object source = e.getSource();
// 判断事件源是哪个按钮
if (source == button1) {
// 调用查询方法
query();
} else if (source == button2) {
// 调用添加方法
add();
} else if (source == button3) {
// 调用修改方法
update();
} else if (source == button4) {
// 调用删除方法
delete();
}
}


public static void main(String[] args) {
new Dictionary();
}
}

效果图

image-20231205214444813

版本2.0

睡觉时我想了一下,两个文本框好像不够用,所以我有加了一个文本域,专门用来信息展示,将展示功能从第二个文本框剥离,并对文本域加上了滑轮,不过我把文本域的Size做得挺大的,所以滑轮可能没有用。

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

// 定义一个类,继承JFrame,实现ActionListener接口
public class Dictionary extends JFrame implements ActionListener {
// 定义组件
private JLabel label1, label2; // 标签
private JTextField text1, text2; // 文本框
private JTextArea text3; // 文本域
private JButton button1, button2, button3, button4; // 按钮
private JPanel panel1, panel2, panel3; // 面板
private Connection conn; // 数据库连接
private PreparedStatement ps; // 预编译语句
private ResultSet rs; // 结果集

// 定义构造方法
public Dictionary() {
// 设置窗口标题
super("中英翻译字典");
// 设置窗口大小
setSize(600, 400);
// 设置窗口居中
setLocationRelativeTo(null);
// 设置窗口关闭时退出程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口布局为边界布局
setLayout(new BorderLayout());

// 初始化组件
label1 = new JLabel("开始输入:");
label2 = new JLabel("翻译结果:");
text1 = new JTextField(20);
text2 = new JTextField(20);
//text2.setEditable(false); // 设置文本框不可编辑
text3 = new JTextArea(10, 40); // 设置文本域的行数和列数
text3.setEditable(false); // 设置文本域不可编辑
text3.setLineWrap(true); // 设置文本域自动换行

button1 = new JButton("查询");
button2 = new JButton("添加");
button3 = new JButton("修改");
button4 = new JButton("删除");
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();

// 添加组件到面板
panel1.add(label1);
panel1.add(text1);
panel2.add(label2);
panel2.add(text2);
panel3.add(button1);
panel3.add(button2);
panel3.add(button3);
panel3.add(button4);

// 添加面板到窗口
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.SOUTH);

// 设置窗口可见
setVisible(true);

// 注册监听器
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);

// 连接数据库
connectDB();
}

// 定义连接数据库的方法
public void connectDB() {
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/luoitheima", "root", "123456");
// 输出提示信息
System.out.println("数据库连接成功!");
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

// 定义查询的方法
public void query() {
try {
// 获取输入的单词或汉字
String input = text1.getText().trim();
// 判断输入是否为空
if (input.equals("")) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "请输入单词或汉字!");
// 清空文本框
text1.setText("");
text2.setText("");
// 返回
return;
}
// 定义查询语句
String sql = "select * from words where word = ? or chinese = ?";
// 预编译语句
ps = conn.prepareStatement(sql);
// 设置参数
ps.setString(1, input);
ps.setString(2, input);
// 执行查询
rs = ps.executeQuery();
// 判断结果集是否为空
if (rs.next()) {
// 获取单词和汉字
String word = rs.getString("word");
String chinese = rs.getString("chinese");
// 判断输入是单词还是汉字
if (input.equals(word)) {
// 设置文本框显示汉字
text2.setText(chinese);
} else {
// 设置文本框显示单词
text2.setText(word);
}
} else {
// 弹出提示框
JOptionPane.showMessageDialog(this, "没有找到对应的翻译!");
// 清空文本框
text2.setText("");
}
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

// 定义添加的方法
public void add() {

// 获取输入的单词和汉字
String word = text1.getText().trim();
String chinese = text2.getText().trim();
System.out.println(word);
System.out.println(chinese);
}

// 定义修改的方法
public void update() {
try {
// 获取输入的单词和汉字
String word = text1.getText().trim();
String chinese = text2.getText().trim();
// 判断输入是否为空
if (word.equals("") || chinese.equals("")) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "请输入单词和汉字!");
// 返回
return;
}
// 定义查询语句
String sql1 = "select * from words where word = ? or chinese = ?";
// 预编译语句
ps = conn.prepareStatement(sql1);
// 设置参数
ps.setString(1, word);
ps.setString(2, chinese);
// 执行查询
rs = ps.executeQuery();
// 判断结果集是否为空
if (!rs.next()) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "该单词或汉字不存在!");
// 清空文本框
text1.setText("");
text2.setText("");
// 返回
return;
}
// 定义更新语句
String sql2 = "update words set chinese = ? where word = ?";
// 预编译语句
ps = conn.prepareStatement(sql2);
// 设置参数
ps.setString(1, chinese);
ps.setString(2, word);
// 执行更新
int n = ps.executeUpdate();
// 判断更新是否成功
if (n > 0) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "修改成功!");
// 清空文本框
text1.setText("");
text2.setText("");
} else {
// 弹出提示框
JOptionPane.showMessageDialog(this, "修改失败!");
}
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

// 定义删除的方法
public void delete() {
try {
// 获取输入的单词或汉字
String input = text1.getText().trim();
// 判断输入是否为空
if (input.equals("")) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "请输入单词或汉字!");
// 清空文本框
text1.setText("");
text2.setText("");
// 返回
return;
}
// 定义查询语句
String sql1 = "select * from words where word = ? or chinese = ?";
// 预编译语句
ps = conn.prepareStatement(sql1);
// 设置参数
ps.setString(1, input);
ps.setString(2, input);
// 执行查询
rs = ps.executeQuery();
// 判断结果集是否为空
if (!rs.next()) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "该单词或汉字不存在!");
// 清空文本框
text1.setText("");
text2.setText("");
// 返回
return;
}
// 定义删除语句
String sql2 = "delete from words where word = ? or chinese = ?";
// 预编译语句
ps = conn.prepareStatement(sql2);
// 设置参数
ps.setString(1, input);
ps.setString(2, input);
// 执行删除
int n = ps.executeUpdate();
// 判断删除是否成功
if (n > 0) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "删除成功!");
// 清空文本框
text1.setText("");
text2.setText("");
} else {
// 弹出提示框
JOptionPane.showMessageDialog(this, "删除失败!");
}
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

@Override
// 定义点击事件函数
public void actionPerformed(ActionEvent e) {
// 获取事件源
Object source = e.getSource();
// 判断事件源是哪个按钮
if (source == button1) {
// 调用查询方法
query();
} else if (source == button2) {
// 调用添加方法
add();
} else if (source == button3) {
// 调用修改方法
update();
} else if (source == button4) {
// 调用删除方法
delete();
}
}


public static void main(String[] args) {
new Dictionary();
}
}

这个版本已经算是可以交作业了,我手上的第三版本就先不在这里发布了,等老师收完作业再发,哈哈,留一手。

最终版

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;

// 定义一个类,继承JFrame,实现ActionListener接口
public class Dictionary extends JFrame implements ActionListener {
// 定义组件
private JLabel label1, label2,label3; // 标签
private String luo1;
private JTextField text1, text2; // 文本框
private JTextArea text3; // 文本域
private JButton button1, button2, button3, button4; // 按钮
private JPanel panel1, panel2, panel3,panel4; // 面板
private JScrollPane scroll;
private Connection conn; // 数据库连接
private PreparedStatement ps; // 预编译语句
private ResultSet rs; // 结果集
private JMenuBar jb;
private JMenu jm;
private JMenuItem about;
private JMenuItem rule;
private JMenuItem all;

// 定义构造方法
public Dictionary() {
// 设置窗口标题
super("丰哥精品中英翻译字典");
JMenu s=new JMenu();
add(s);
// 设置窗口大小
setSize(425, 450);
// 设置窗口居中
setLocationRelativeTo(null);
// 设置窗口关闭时退出程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 设置窗口布局为绝对布局
setLayout(null);
//菜单
JMenuBar mb = new JMenuBar();
JMenu help = new JMenu("Help");
about = new JMenuItem("About");
about.addActionListener(this);
rule=new JMenuItem("规则");
rule.addActionListener(this);
all=new JMenuItem("全部数据源");
all.addActionListener(this);
help.add(all);
help.add(rule);
help.add(about);
mb.add(help);
setJMenuBar(mb);
setLayout(null);
setVisible(true);


// 初始化组件
label1 = new JLabel("输入单词:");
label2 = new JLabel("输入汉字:");
label3 =new JLabel("结果:");
text1 = new JTextField(20);
text2 = new JTextField(20);
//text2.setEditable(false); // 设置文本框不可编辑
text3 = new JTextArea(15, 25); // 设置文本域的行数和列数
text3.setEditable(false); // 设置文本域不可编辑
text3.setLineWrap(true); // 设置文本域自动换行

button1 = new JButton("查询");
button2 = new JButton("添加");
button3 = new JButton("修改");
button4 = new JButton("删除");
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
scroll = new JScrollPane(text3); // 将文本域添加到滚动条中

// 添加组件到面板
panel1.add(label1);
panel1.add(text1);
panel2.add(label2);
panel2.add(text2);
panel3.add(button1);
panel3.add(button2);
panel3.add(button3);
panel3.add(button4);
panel4.add(label3);
panel4.add(scroll);

// 设置面板的位置和大小
panel1.setBounds(50, 0, 300, 50);
panel2.setBounds(50, 50, 300, 50);
panel3.setBounds(50, 100, 300, 50);
panel4.setBounds(50, 150, 300, 200);

// 添加面板到窗口
add(panel1);
add(panel2);
add(panel3);
add(panel4);

// 设置窗口可见
setVisible(true);

// 注册监听器
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);

// 连接数据库
connectDB();
}

// 定义连接数据库的方法
public void connectDB() {
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/luoitheima", "root", "123456");
// 输出提示信息
System.out.println("数据库连接成功!");
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

// 定义查询的方法
public void query() {
try {
// 获取输入的单词或汉字
String input = text1.getText().trim();
// 判断输入是否为空
if (input.equals("")) {
// 清空文本框
text1.setText("");
text2.setText("");
text3.setText("请在最上面的文本框写要查询的数据");
// 返回
return;
}
// 定义查询语句
String sql = "select * from words where word = ? or chinese = ?";
// 预编译语句
ps = conn.prepareStatement(sql);
// 设置参数
ps.setString(1, input);
ps.setString(2, input);
// 执行查询
rs = ps.executeQuery();
// 判断结果集是否为空
if (rs.next()) {
// 获取单词和汉字
String word = rs.getString("word");
String chinese = rs.getString("chinese");
text3.setText(word+"\t\t"+chinese+"\n");
} else {
// 清空文本框
text2.setText("");
text3.setText("没有找到对应的翻译!\n");
}
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

// 定义添加的方法
public void add() {
try {
// 获取输入的单词和汉字,分别赋值给变量word和chinese。
String word = text1.getText().trim();
//判断输入的是否是单词()
if(word.getBytes().length!=word.length()){
JOptionPane.showMessageDialog(this, "大哥你看清楚点!!输单词啊");
text1.setText("");
text2.setText("");
return;
}
String chinese = text2.getText().trim();
if(chinese.getBytes().length==word.length()){
JOptionPane.showMessageDialog(this, "大哥你看清楚点!!输汉字啊");
text1.setText("");
text2.setText("");
return;
}
// 判断输入是否为空,如果为空,弹出提示框,清空文本框,返回。
if (word.equals("") || chinese.equals("")) {
// 清空文本框
text1.setText("");
text2.setText("");
text3.setText("请输入单词和汉字!\n");
// 返回
return;
}
// 定义查询语句,用来检查数据库中是否已经存在该单词或汉字,如果存在,弹出提示框,清空文本框,返回。
String sql1 = "select * from words where word = '"+word+"' or chinese ='"+chinese+"'";
// 预编译语句
ps = conn.prepareStatement(sql1);
// 执行查询
rs = ps.executeQuery();
// 判断结果集是否为空
if (rs.next()) {
// 清空文本框
text1.setText("");
text2.setText("");
text3.setText("该单词或汉字已存在!\n");
// 返回
return;
}
// 定义插入语句,用来向数据库中插入单词和汉字,设置参数,执行插入。
String sql2 = "insert into words values ('"+word+"', '"+chinese+"')";
// 预编译语句
ps = conn.prepareStatement(sql2);
// 执行插入
int n = ps.executeUpdate();
// 判断插入是否成功,如果成功,弹出提示框,清空文本框,如果失败,弹出提示框。
if (n > 0) {
// 清空文本框
text1.setText("");
text2.setText("");
text3.setText("添加成功");
} else {
text3.setText("添加失败");
}
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

// 定义修改的方法
public void update() {
try {
// 获取输入的单词和汉字
String word = text1.getText().trim();
String chinese = text2.getText().trim();
// 判断输入是否为空
if (word.equals("") || chinese.equals("")) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "请输入单词和汉字!");
// 返回
return;
}
// 定义查询语句
String sql1 = "select * from words where word = ? or chinese = ?";
// 预编译语句
ps = conn.prepareStatement(sql1);
// 设置参数
ps.setString(1, word);
ps.setString(2, chinese);
// 执行查询
rs = ps.executeQuery();
// 判断结果集是否为空
if (!rs.next()) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "该单词或汉字不存在!");
// 清空文本框
text1.setText("");
text2.setText("");
// 返回
return;
}
// 定义更新语句
String sql2 = "update words set chinese = ? ,word= ? where word = ? or chinese= ?";
// 预编译语句
ps = conn.prepareStatement(sql2);
// 设置参数
ps.setString(1, chinese);
ps.setString(2, word);
ps.setString(3,word);
ps.setString(4,chinese);
// 执行更新
int n = ps.executeUpdate();
// 判断更新是否成功
if (n > 0) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "修改成功!");
// 清空文本框
text1.setText("");
text2.setText("");
} else {
// 弹出提示框
JOptionPane.showMessageDialog(this, "修改失败!");
}
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

// 定义删除的方法
public void delete() {
try {
// 获取输入的单词或汉字
String input = text1.getText().trim();
// 判断输入是否为空
if (input.equals("")) {
text3.append("请在顶框输入单词或汉字");
// 清空文本框
text1.setText("");
text2.setText("");
// 返回
return;
}
// 定义查询语句
String sql1 = "select * from words where word = ? or chinese = ?";
// 预编译语句
ps = conn.prepareStatement(sql1);
// 设置参数
ps.setString(1, input);
ps.setString(2, input);
// 执行查询
rs = ps.executeQuery();
// 判断结果集是否为空
if (!rs.next()) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "该单词或汉字不存在!");
// 清空文本框
text1.setText("");
text2.setText("");
// 返回
return;
}else {
String word = rs.getString("word");
String chinese = rs.getString("chinese");
text3.append("开始删除 "+word+" -- "+chinese+"\n");
}
// 定义删除语句
String sql2 = "delete from words where word = ? or chinese = ?";
// 预编译语句
ps = conn.prepareStatement(sql2);
// 设置参数
ps.setString(1, input);
ps.setString(2, input);
// 执行删除
int n = ps.executeUpdate();
// 判断删除是否成功
if (n > 0) {
// 弹出提示框
JOptionPane.showMessageDialog(this, "删除成功!");
// 清空文本框
text1.setText("");
text2.setText("");
} else {
// 弹出提示框
JOptionPane.showMessageDialog(this, "删除失败!");
}
} catch (Exception e) {
// 输出异常信息
e.printStackTrace();
}
}

@Override
// 定义点击事件函数
public void actionPerformed(ActionEvent e) {
// 获取事件源
Object source = e.getSource();
System.out.println(source);
// 判断事件源是哪个按钮
if (source == button1) {
// 调用查询方法
query();
} else if (source == button2) {
// 调用添加方法
add();
} else if (source == button3) {
// 调用修改方法
update();
} else if (source == button4) {
// 调用删除方法
delete();
}else if(source==about){
//弹框对象
JDialog jd=new JDialog();
//管理图片容器
JLabel jb1=new JLabel(new ImageIcon(".\\1.jpg"));
jb1.setBounds(0,0,700,700);
jd.getContentPane().add(jb1);
jd.setSize(800,800);
jd.setAlwaysOnTop(true);
//居中
jd.setLocationRelativeTo(null);
jd.setModal(true);
jd.setVisible(true);
}else if(source==rule){
JOptionPane.showMessageDialog(this, "除了更改和添加数据,其他情况都只能在第一个文本框输入!!!");
}else if(source==all){
try {
// 定义查询语句
String sql = "select * from words";
// 预编译语句
ps = conn.prepareStatement(sql);
// 执行查询
rs = ps.executeQuery();
// 判断结果集是否为空
int k=1;
text3.setText("");
while (rs.next()){
String word = rs.getString("word");
String chinese = rs.getString("chinese");
text3.append(k+"."+word+"\t\t"+chinese+"\n");
k++;
}
}catch (Exception s){
s.printStackTrace();
}
}
}


public static void main(String[] args) {
new Dictionary();
}
}

运行效果

image-20231209131829691

更新于

请我喝[茶]~( ̄▽ ̄)~*

罗梓丰 微信支付

微信支付

罗梓丰 支付宝

支付宝