質問編集履歴
1
ソースコードの表示を更新しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
|
5
|
+
SQLで郵便番号のデータベース(No.,郵便番号,県,市,地区,県の読み,市の読み,地区の読み)を用いて距離を出すプログラムを作っている最中です。
|
6
6
|
|
7
7
|
とりあえず住所を三つ表示させたいです。
|
8
8
|
|
@@ -16,500 +16,496 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
-
|
19
|
+
### 該当のソースコード
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
Java
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
```html
|
28
|
+
|
29
|
+
コードの表示(ブロック)
|
30
|
+
|
31
|
+
public class postNumberGUI extends Frame implements ActionListener, ItemListener {
|
32
|
+
|
33
|
+
java.awt.List codeList;
|
34
|
+
|
35
|
+
int n=3;
|
36
|
+
|
37
|
+
int a=0;
|
38
|
+
|
39
|
+
int x=0;
|
40
|
+
|
41
|
+
TextField[] codeField = new TextField[n];
|
42
|
+
|
43
|
+
TextField[] numberField = new TextField[n];・・・
|
44
|
+
|
45
|
+
TextField serchField;
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
Button displayButton,・・・
|
50
|
+
|
51
|
+
Panel panel, ・・・
|
52
|
+
|
53
|
+
//Panel[] dataPanel;
|
54
|
+
|
55
|
+
String displayCommand = "Display",
|
56
|
+
|
57
|
+
addCommand = "Add",
|
58
|
+
|
59
|
+
serchCommand = "Serch",
|
60
|
+
|
61
|
+
doneCommand = "Done";
|
62
|
+
|
63
|
+
// updateCommand = "Update",addCommand = "Add", deleteCommand = "Delete"; // ボタンのコマンド文字列
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
String driverClassName = "org.postgresql.Driver"; //SQL
|
68
|
+
|
69
|
+
String url = "jdbc:postgresql://localhost/test";
|
70
|
+
|
71
|
+
String user = "dbpuser";
|
72
|
+
|
73
|
+
String password = "hogehoge";
|
74
|
+
|
75
|
+
Connection connection;
|
76
|
+
|
77
|
+
ResultSet resultSet;
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
//郵便番号で検索
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
PreparedStatement prepStmt; // SELECT name 用 (リスト表示)
|
88
|
+
|
89
|
+
PreparedStatement prepStmt_S; // SELECT用
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
String selectStr = "SELECT code FROM postalcode";
|
94
|
+
|
95
|
+
String strPrepSQL_S = "SELECT * FROM postalcode WHERE code = ?";
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
postNumberGUI(){
|
100
|
+
|
101
|
+
setSize(1200,1000);
|
102
|
+
|
103
|
+
setTitle("postNumberGUI");
|
104
|
+
|
105
|
+
setLayout(new GridLayout(1,2));
|
106
|
+
|
107
|
+
System.out.println(a);
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
panel = new Panel();
|
112
|
+
|
113
|
+
panel.setLayout(new BorderLayout());
|
114
|
+
|
115
|
+
add(panel);
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
leftPanel = new Panel();
|
120
|
+
|
121
|
+
leftPanel.setLayout(new GridLayout());
|
122
|
+
|
123
|
+
add(leftPanel);
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
codeList = new java.awt.List(10);
|
128
|
+
|
129
|
+
codeList.addItemListener(this);
|
130
|
+
|
131
|
+
leftPanel.add(codeList);
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
rightPanel = new Panel();
|
138
|
+
|
139
|
+
rightPanel.setLayout(new GridLayout(2,4));
|
140
|
+
|
141
|
+
add(rightPanel);
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
dataPanel1 = new Panel();
|
146
|
+
|
147
|
+
dataPanel1.setLayout(new GridLayout(8,2));
|
148
|
+
|
149
|
+
rightPanel.add(dataPanel1);
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
dataPanel2 = new Panel();
|
154
|
+
|
155
|
+
dataPanel2.setLayout(new GridLayout(8,2));
|
156
|
+
|
157
|
+
rightPanel.add(dataPanel2);
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
dataPanel3 = new Panel();
|
162
|
+
|
163
|
+
dataPanel3.setLayout(new GridLayout(8,2));
|
164
|
+
|
165
|
+
rightPanel.add(dataPanel3);
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
serchPanel = new Panel();
|
170
|
+
|
171
|
+
serchPanel.setLayout(new BorderLayout());
|
172
|
+
|
173
|
+
//rightPanel.add(serchPanel);
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
buttonPanel = new Panel();
|
178
|
+
|
179
|
+
buttonPanel.setLayout(new GridLayout(2,2));
|
180
|
+
|
181
|
+
rightPanel.add(buttonPanel);
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
panel.add(leftPanel, BorderLayout.WEST);
|
186
|
+
|
187
|
+
panel.add(rightPanel, BorderLayout.CENTER);
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
for(int i=0;i<n;i++) {
|
194
|
+
|
195
|
+
if(i==0){
|
196
|
+
|
197
|
+
dataPanel1.add(new Label("郵便番号"));
|
198
|
+
|
199
|
+
codeField[i] = new TextField(24);
|
200
|
+
|
201
|
+
dataPanel1.add(codeField[i]);
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
dataPanel1.add(new Label("Number"));
|
206
|
+
|
207
|
+
numberField[i] = new TextField(24);
|
208
|
+
|
209
|
+
dataPanel1.add(numberField[i]);・・・
|
210
|
+
|
211
|
+
}else if(i==1){
|
212
|
+
|
213
|
+
dataPanel2.add(new Label("郵便番号"));
|
214
|
+
|
215
|
+
codeField[i] = new TextField(24);
|
216
|
+
|
217
|
+
dataPanel2.add(codeField[i]);・・・
|
218
|
+
|
219
|
+
}else if(i==2){
|
220
|
+
|
221
|
+
dataPanel3.add(new Label("郵便番号"));
|
222
|
+
|
223
|
+
codeField[i] = new TextField(24);
|
224
|
+
|
225
|
+
dataPanel3.add(codeField[i]);・・・
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
serchPanel.add(new Label("検索"));
|
232
|
+
|
233
|
+
serchField = new TextField(24);
|
234
|
+
|
235
|
+
serchPanel.add(serchField, BorderLayout.CENTER);
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
doneButton = new Button(doneCommand);
|
240
|
+
|
241
|
+
doneButton.addActionListener(this);
|
242
|
+
|
243
|
+
buttonPanel.add(doneButton);
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
displayButton = new Button(displayCommand);
|
248
|
+
|
249
|
+
displayButton.addActionListener(this);
|
250
|
+
|
251
|
+
buttonPanel.add(displayButton);
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
addButton = new Button(addCommand);
|
256
|
+
|
257
|
+
addButton.addActionListener(this);
|
258
|
+
|
259
|
+
buttonPanel.add(addButton);
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
serchButton = new Button(serchCommand);
|
264
|
+
|
265
|
+
serchButton.addActionListener(this);
|
266
|
+
|
267
|
+
serchPanel.add(serchButton, BorderLayout.EAST);
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
rightPanel.add(serchPanel);
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
addWindowListener ( new WindowAdapter() {
|
276
|
+
|
277
|
+
public void windowClosing(WindowEvent we) {
|
278
|
+
|
279
|
+
try { // 後処理
|
280
|
+
|
281
|
+
prepStmt.close();
|
282
|
+
|
283
|
+
prepStmt_S.close();
|
284
|
+
|
285
|
+
connection.close();
|
286
|
+
|
287
|
+
} catch (Exception e) {
|
288
|
+
|
289
|
+
e.printStackTrace();
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
System.exit(0);
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
} ) ; // ウィンドウを閉じる処理
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
try { // ドライバマネージャとコネクション
|
302
|
+
|
303
|
+
Class.forName(driverClassName);
|
304
|
+
|
305
|
+
connection = DriverManager.getConnection(url, user, password);
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
prepStmt = connection.prepareStatement(selectStr);
|
310
|
+
|
311
|
+
prepStmt_S = connection.prepareStatement(strPrepSQL_S);
|
312
|
+
|
313
|
+
} catch (Exception e) {
|
314
|
+
|
315
|
+
e.printStackTrace();
|
316
|
+
|
317
|
+
}
|
318
|
+
|
319
|
+
displayList(); // 早速リストに名前表示
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
public void itemStateChanged(ItemEvent ie) { // 選択項目が変化した時の処理
|
328
|
+
|
329
|
+
displayData(); // 各フィールドにデータ表示
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
public void clearList() { // リストクリア
|
334
|
+
|
335
|
+
codeList.removeAll(); // リストの項目をすべて削除
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
public void displayList() { // リスト項目表示
|
342
|
+
|
343
|
+
try {
|
344
|
+
|
345
|
+
resultSet = prepStmt.executeQuery(); // コードの列だけ抜き出す
|
346
|
+
|
347
|
+
while (resultSet.next()) {
|
348
|
+
|
349
|
+
String code = resultSet.getString("code");
|
350
|
+
|
351
|
+
codeList.add(code); // リストにコードを追加
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
resultSet.close();
|
356
|
+
|
357
|
+
} catch (Exception e) {
|
358
|
+
|
359
|
+
e.printStackTrace();
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
public void displayData() { // 選択項目データ再表示
|
368
|
+
|
369
|
+
String code = codeList.getSelectedItem(), // 名前はリストの選択項目
|
370
|
+
|
371
|
+
number = "",
|
372
|
+
|
373
|
+
prefecture = "",
|
374
|
+
|
375
|
+
city = "",
|
376
|
+
|
377
|
+
area = "",
|
378
|
+
|
379
|
+
pref_reading = "",
|
380
|
+
|
381
|
+
city_reading = "",
|
382
|
+
|
383
|
+
area_reading = "";
|
384
|
+
|
385
|
+
try {
|
386
|
+
|
387
|
+
prepStmt_S.setString(1, code);
|
388
|
+
|
389
|
+
resultSet = prepStmt_S.executeQuery();
|
390
|
+
|
391
|
+
while (resultSet.next()) {
|
392
|
+
|
393
|
+
code = resultSet.getString("code");・・・;
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
while(0<=a&&a<=2) {
|
398
|
+
|
399
|
+
codeField[a].setText(code);
|
400
|
+
|
401
|
+
numberField[a].setText(number);・・・;
|
402
|
+
|
403
|
+
resultSet.close();
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
} catch (Exception e) {
|
408
|
+
|
409
|
+
e.printStackTrace();
|
410
|
+
|
411
|
+
}
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
public void serchData() { // 項目データ追加
|
418
|
+
|
419
|
+
String code = codeList.getSelectedItem(), // 名前はリストの選択項目
|
420
|
+
|
421
|
+
number = "",
|
422
|
+
|
423
|
+
prefecture = "",
|
424
|
+
|
425
|
+
city = "",
|
426
|
+
|
427
|
+
area = "",
|
428
|
+
|
429
|
+
pref_reading = "",
|
430
|
+
|
431
|
+
city_reading = "",
|
432
|
+
|
433
|
+
area_reading = "";
|
434
|
+
|
435
|
+
code = serchField.getText(); // 各データをもらう
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
serchField.setText("");
|
440
|
+
|
441
|
+
try { // 新データを追加
|
442
|
+
|
443
|
+
prepStmt_S.setString(1, code);
|
444
|
+
|
445
|
+
resultSet = prepStmt_S.executeQuery();
|
446
|
+
|
447
|
+
while (resultSet.next()) {
|
448
|
+
|
449
|
+
code = resultSet.getString("code");・・・;
|
450
|
+
|
451
|
+
}
|
452
|
+
|
453
|
+
while(0<=a&&a<=2) {
|
454
|
+
|
455
|
+
codeField[a].setText(code); ・・・;
|
456
|
+
|
457
|
+
resultSet.close();
|
458
|
+
|
459
|
+
}
|
460
|
+
|
461
|
+
} catch (Exception e) {
|
462
|
+
|
463
|
+
e.printStackTrace();
|
464
|
+
|
465
|
+
}
|
466
|
+
|
467
|
+
}
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
public void actionPerformed(ActionEvent ae) { // ボタンが押された時に行う処理
|
472
|
+
|
473
|
+
String command = ae.getActionCommand(); // イベントからアクションコマンドを得る
|
474
|
+
|
475
|
+
if (command.equals(displayCommand)) {
|
476
|
+
|
477
|
+
displayData(); // 表示処理
|
478
|
+
|
479
|
+
}else if(command.equals(doneCommand)) {
|
480
|
+
|
481
|
+
a++;
|
482
|
+
|
483
|
+
System.out.println(a);
|
484
|
+
|
485
|
+
}else if(command.equals(serchCommand)) {
|
486
|
+
|
487
|
+
serchData();
|
488
|
+
|
489
|
+
a++;
|
490
|
+
|
491
|
+
System.out.println(a);
|
492
|
+
|
493
|
+
}
|
494
|
+
|
495
|
+
}
|
496
|
+
|
497
|
+
public static void main(String[] argv) {
|
498
|
+
|
499
|
+
postNumberGUI myFrame = new postNumberGUI(); // フレームの生成
|
500
|
+
|
501
|
+
myFrame.setVisible(true); // フレームの可視化
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
}
|
20
506
|
|
21
507
|
```
|
22
508
|
|
23
|
-
エラーメッセージ
|
24
|
-
|
25
|
-
```
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
### 該当のソースコード
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
Java
|
34
|
-
|
35
|
-
public class postNumberGUI extends Frame implements ActionListener, ItemListener {
|
36
|
-
|
37
|
-
java.awt.List codeList;
|
38
|
-
|
39
|
-
int n=3;
|
40
|
-
|
41
|
-
int a=0;
|
42
|
-
|
43
|
-
int x=0;
|
44
|
-
|
45
|
-
TextField[] codeField = new TextField[n];
|
46
|
-
|
47
|
-
TextField[] numberField = new TextField[n];・・・
|
48
|
-
|
49
|
-
TextField serchField;
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
Button displayButton,・・・
|
54
|
-
|
55
|
-
Panel panel, ・・・
|
56
|
-
|
57
|
-
//Panel[] dataPanel;
|
58
|
-
|
59
|
-
String displayCommand = "Display",
|
60
|
-
|
61
|
-
addCommand = "Add",
|
62
|
-
|
63
|
-
serchCommand = "Serch",
|
64
|
-
|
65
|
-
doneCommand = "Done";
|
66
|
-
|
67
|
-
// updateCommand = "Update",addCommand = "Add", deleteCommand = "Delete"; // ボタンのコマンド文字列
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
String driverClassName = "org.postgresql.Driver"; //SQL
|
72
|
-
|
73
|
-
String url = "jdbc:postgresql://localhost/test";
|
74
|
-
|
75
|
-
String user = "dbpuser";
|
76
|
-
|
77
|
-
String password = "hogehoge";
|
78
|
-
|
79
|
-
Connection connection;
|
80
|
-
|
81
|
-
ResultSet resultSet;
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
//郵便番号で検索
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
PreparedStatement prepStmt; // SELECT name 用 (リスト表示)
|
92
|
-
|
93
|
-
PreparedStatement prepStmt_S; // SELECT用
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
String selectStr = "SELECT code FROM postalcode";
|
98
|
-
|
99
|
-
String strPrepSQL_S = "SELECT * FROM postalcode WHERE code = ?";
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
postNumberGUI(){
|
104
|
-
|
105
|
-
setSize(1200,1000);
|
106
|
-
|
107
|
-
setTitle("postNumberGUI");
|
108
|
-
|
109
|
-
setLayout(new GridLayout(1,2));
|
110
|
-
|
111
|
-
System.out.println(a);
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
panel = new Panel();
|
116
|
-
|
117
|
-
panel.setLayout(new BorderLayout());
|
118
|
-
|
119
|
-
add(panel);
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
leftPanel = new Panel();
|
124
|
-
|
125
|
-
leftPanel.setLayout(new GridLayout());
|
126
|
-
|
127
|
-
add(leftPanel);
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
codeList = new java.awt.List(10);
|
132
|
-
|
133
|
-
codeList.addItemListener(this);
|
134
|
-
|
135
|
-
leftPanel.add(codeList);
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
rightPanel = new Panel();
|
142
|
-
|
143
|
-
rightPanel.setLayout(new GridLayout(2,4));
|
144
|
-
|
145
|
-
add(rightPanel);
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
dataPanel1 = new Panel();
|
150
|
-
|
151
|
-
dataPanel1.setLayout(new GridLayout(8,2));
|
152
|
-
|
153
|
-
rightPanel.add(dataPanel1);
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
dataPanel2 = new Panel();
|
158
|
-
|
159
|
-
dataPanel2.setLayout(new GridLayout(8,2));
|
160
|
-
|
161
|
-
rightPanel.add(dataPanel2);
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
dataPanel3 = new Panel();
|
166
|
-
|
167
|
-
dataPanel3.setLayout(new GridLayout(8,2));
|
168
|
-
|
169
|
-
rightPanel.add(dataPanel3);
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
serchPanel = new Panel();
|
174
|
-
|
175
|
-
serchPanel.setLayout(new BorderLayout());
|
176
|
-
|
177
|
-
//rightPanel.add(serchPanel);
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
buttonPanel = new Panel();
|
182
|
-
|
183
|
-
buttonPanel.setLayout(new GridLayout(2,2));
|
184
|
-
|
185
|
-
rightPanel.add(buttonPanel);
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
panel.add(leftPanel, BorderLayout.WEST);
|
190
|
-
|
191
|
-
panel.add(rightPanel, BorderLayout.CENTER);
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
for(int i=0;i<n;i++) {
|
198
|
-
|
199
|
-
if(i==0){
|
200
|
-
|
201
|
-
dataPanel1.add(new Label("郵便番号"));
|
202
|
-
|
203
|
-
codeField[i] = new TextField(24);
|
204
|
-
|
205
|
-
dataPanel1.add(codeField[i]);
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
dataPanel1.add(new Label("Number"));
|
210
|
-
|
211
|
-
numberField[i] = new TextField(24);
|
212
|
-
|
213
|
-
dataPanel1.add(numberField[i]);・・・
|
214
|
-
|
215
|
-
}else if(i==1){
|
216
|
-
|
217
|
-
dataPanel2.add(new Label("郵便番号"));
|
218
|
-
|
219
|
-
codeField[i] = new TextField(24);
|
220
|
-
|
221
|
-
dataPanel2.add(codeField[i]);・・・
|
222
|
-
|
223
|
-
}else if(i==2){
|
224
|
-
|
225
|
-
dataPanel3.add(new Label("郵便番号"));
|
226
|
-
|
227
|
-
codeField[i] = new TextField(24);
|
228
|
-
|
229
|
-
dataPanel3.add(codeField[i]);・・・
|
230
|
-
|
231
|
-
}
|
232
|
-
|
233
|
-
}
|
234
|
-
|
235
|
-
serchPanel.add(new Label("検索"));
|
236
|
-
|
237
|
-
serchField = new TextField(24);
|
238
|
-
|
239
|
-
serchPanel.add(serchField, BorderLayout.CENTER);
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
doneButton = new Button(doneCommand);
|
244
|
-
|
245
|
-
doneButton.addActionListener(this);
|
246
|
-
|
247
|
-
buttonPanel.add(doneButton);
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
displayButton = new Button(displayCommand);
|
252
|
-
|
253
|
-
displayButton.addActionListener(this);
|
254
|
-
|
255
|
-
buttonPanel.add(displayButton);
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
addButton = new Button(addCommand);
|
260
|
-
|
261
|
-
addButton.addActionListener(this);
|
262
|
-
|
263
|
-
buttonPanel.add(addButton);
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
serchButton = new Button(serchCommand);
|
268
|
-
|
269
|
-
serchButton.addActionListener(this);
|
270
|
-
|
271
|
-
serchPanel.add(serchButton, BorderLayout.EAST);
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
rightPanel.add(serchPanel);
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
addWindowListener ( new WindowAdapter() {
|
280
|
-
|
281
|
-
public void windowClosing(WindowEvent we) {
|
282
|
-
|
283
|
-
try { // 後処理
|
284
|
-
|
285
|
-
prepStmt.close();
|
286
|
-
|
287
|
-
prepStmt_S.close();
|
288
|
-
|
289
|
-
connection.close();
|
290
|
-
|
291
|
-
} catch (Exception e) {
|
292
|
-
|
293
|
-
e.printStackTrace();
|
294
|
-
|
295
|
-
}
|
296
|
-
|
297
|
-
System.exit(0);
|
298
|
-
|
299
|
-
}
|
300
|
-
|
301
|
-
} ) ; // ウィンドウを閉じる処理
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
try { // ドライバマネージャとコネクション
|
306
|
-
|
307
|
-
Class.forName(driverClassName);
|
308
|
-
|
309
|
-
connection = DriverManager.getConnection(url, user, password);
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
prepStmt = connection.prepareStatement(selectStr);
|
314
|
-
|
315
|
-
prepStmt_S = connection.prepareStatement(strPrepSQL_S);
|
316
|
-
|
317
|
-
} catch (Exception e) {
|
318
|
-
|
319
|
-
e.printStackTrace();
|
320
|
-
|
321
|
-
}
|
322
|
-
|
323
|
-
displayList(); // 早速リストに名前表示
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
}
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
public void itemStateChanged(ItemEvent ie) { // 選択項目が変化した時の処理
|
332
|
-
|
333
|
-
displayData(); // 各フィールドにデータ表示
|
334
|
-
|
335
|
-
}
|
336
|
-
|
337
|
-
public void clearList() { // リストクリア
|
338
|
-
|
339
|
-
codeList.removeAll(); // リストの項目をすべて削除
|
340
|
-
|
341
|
-
}
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
public void displayList() { // リスト項目表示
|
346
|
-
|
347
|
-
try {
|
348
|
-
|
349
|
-
resultSet = prepStmt.executeQuery(); // コードの列だけ抜き出す
|
350
|
-
|
351
|
-
while (resultSet.next()) {
|
352
|
-
|
353
|
-
String code = resultSet.getString("code");
|
354
|
-
|
355
|
-
codeList.add(code); // リストにコードを追加
|
356
|
-
|
357
|
-
}
|
358
|
-
|
359
|
-
resultSet.close();
|
360
|
-
|
361
|
-
} catch (Exception e) {
|
362
|
-
|
363
|
-
e.printStackTrace();
|
364
|
-
|
365
|
-
}
|
366
|
-
|
367
|
-
}
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
public void displayData() { // 選択項目データ再表示
|
372
|
-
|
373
|
-
String code = codeList.getSelectedItem(), // 名前はリストの選択項目
|
374
|
-
|
375
|
-
number = "",
|
376
|
-
|
377
|
-
prefecture = "",
|
378
|
-
|
379
|
-
city = "",
|
380
|
-
|
381
|
-
area = "",
|
382
|
-
|
383
|
-
pref_reading = "",
|
384
|
-
|
385
|
-
city_reading = "",
|
386
|
-
|
387
|
-
area_reading = "";
|
388
|
-
|
389
|
-
try {
|
390
|
-
|
391
|
-
prepStmt_S.setString(1, code);
|
392
|
-
|
393
|
-
resultSet = prepStmt_S.executeQuery();
|
394
|
-
|
395
|
-
while (resultSet.next()) {
|
396
|
-
|
397
|
-
code = resultSet.getString("code");・・・;
|
398
|
-
|
399
|
-
}
|
400
|
-
|
401
|
-
while(0<=a&&a<=2) {
|
402
|
-
|
403
|
-
codeField[a].setText(code);
|
404
|
-
|
405
|
-
numberField[a].setText(number);・・・;
|
406
|
-
|
407
|
-
resultSet.close();
|
408
|
-
|
409
|
-
}
|
410
|
-
|
411
|
-
} catch (Exception e) {
|
412
|
-
|
413
|
-
e.printStackTrace();
|
414
|
-
|
415
|
-
}
|
416
|
-
|
417
|
-
}
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
public void serchData() { // 項目データ追加
|
422
|
-
|
423
|
-
String code = codeList.getSelectedItem(), // 名前はリストの選択項目
|
424
|
-
|
425
|
-
number = "",
|
426
|
-
|
427
|
-
prefecture = "",
|
428
|
-
|
429
|
-
city = "",
|
430
|
-
|
431
|
-
area = "",
|
432
|
-
|
433
|
-
pref_reading = "",
|
434
|
-
|
435
|
-
city_reading = "",
|
436
|
-
|
437
|
-
area_reading = "";
|
438
|
-
|
439
|
-
code = serchField.getText(); // 各データをもらう
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
serchField.setText("");
|
444
|
-
|
445
|
-
try { // 新データを追加
|
446
|
-
|
447
|
-
prepStmt_S.setString(1, code);
|
448
|
-
|
449
|
-
resultSet = prepStmt_S.executeQuery();
|
450
|
-
|
451
|
-
while (resultSet.next()) {
|
452
|
-
|
453
|
-
code = resultSet.getString("code");・・・;
|
454
|
-
|
455
|
-
}
|
456
|
-
|
457
|
-
while(0<=a&&a<=2) {
|
458
|
-
|
459
|
-
codeField[a].setText(code); ・・・;
|
460
|
-
|
461
|
-
resultSet.close();
|
462
|
-
|
463
|
-
}
|
464
|
-
|
465
|
-
} catch (Exception e) {
|
466
|
-
|
467
|
-
e.printStackTrace();
|
468
|
-
|
469
|
-
}
|
470
|
-
|
471
|
-
}
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
public void actionPerformed(ActionEvent ae) { // ボタンが押された時に行う処理
|
476
|
-
|
477
|
-
String command = ae.getActionCommand(); // イベントからアクションコマンドを得る
|
478
|
-
|
479
|
-
if (command.equals(displayCommand)) {
|
480
|
-
|
481
|
-
displayData(); // 表示処理
|
482
|
-
|
483
|
-
}else if(command.equals(doneCommand)) {
|
484
|
-
|
485
|
-
a++;
|
486
|
-
|
487
|
-
System.out.println(a);
|
488
|
-
|
489
|
-
}else if(command.equals(serchCommand)) {
|
490
|
-
|
491
|
-
serchData();
|
492
|
-
|
493
|
-
a++;
|
494
|
-
|
495
|
-
System.out.println(a);
|
496
|
-
|
497
|
-
}
|
498
|
-
|
499
|
-
}
|
500
|
-
|
501
|
-
public static void main(String[] argv) {
|
502
|
-
|
503
|
-
postNumberGUI myFrame = new postNumberGUI(); // フレームの生成
|
504
|
-
|
505
|
-
myFrame.setVisible(true); // フレームの可視化
|
506
|
-
|
507
|
-
}
|
508
|
-
|
509
|
-
}
|
510
|
-
|
511
|
-
|
512
|
-
|
513
509
|
### 試したこと
|
514
510
|
|
515
511
|
|