質問編集履歴
2
意図的に内容を抹消する行為にあたるため
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
プルダウンをPOST送信後に選択した項目のまま保持させたい。
|
test
CHANGED
@@ -1 +1,543 @@
|
|
1
|
+
いつもお世話になっております。
|
2
|
+
|
3
|
+
プルダウンで項目名を選択後にボタンのクリックで送信し、その項目名に合致する情報を表示するプログラムを作成しております。
|
4
|
+
|
5
|
+
送信後のプルダウンを、選択した項目が初期状態になり、他の項目名もプルダウンにて確認できるようにしたいです。
|
6
|
+
|
7
|
+
追加したコードで初期値を設定したのですが、これではプルダウンに1件しか表示されないため、初期値を設定しつつ他の項目をプルダウン内に表示したいのですが、
|
8
|
+
|
9
|
+
どのように修正、記述をすればよいのかが分からないためご教示いただければ幸いです。
|
10
|
+
|
11
|
+
よろしくお願いいたします。
|
12
|
+
|
13
|
+
###表示画面
|
14
|
+
|
15
|
+
```jsp
|
16
|
+
|
17
|
+
<%@ page contentType="text/html; charset=Windows-31J"%>
|
18
|
+
|
19
|
+
<%@ page import="jp.co.keyaki.bean.KuyakushoBean"%>
|
20
|
+
|
21
|
+
<%@ page import="java.util.List"%>
|
22
|
+
|
23
|
+
<%@ page import="java.util.ArrayList"%>
|
24
|
+
|
25
|
+
<% @SuppressWarnings("unchecked")
|
26
|
+
|
27
|
+
List<KuyakushoBean> list = (List<KuyakushoBean>) request.getAttribute("LIST");
|
28
|
+
|
29
|
+
%>
|
30
|
+
|
31
|
+
<% @SuppressWarnings("unchecked")
|
32
|
+
|
33
|
+
List<KuyakushoBean> name = (List<KuyakushoBean>) request.getAttribute("ku");
|
34
|
+
|
35
|
+
%>
|
36
|
+
|
37
|
+
<html>
|
38
|
+
|
39
|
+
<head>
|
40
|
+
|
41
|
+
<meta charset="UTF-8">
|
42
|
+
|
43
|
+
<title>23区区役所一覧</title>
|
44
|
+
|
45
|
+
</head>
|
46
|
+
|
47
|
+
<body>
|
48
|
+
|
1
|
-
|
49
|
+
<div align="center">
|
50
|
+
|
51
|
+
23区区役所一覧
|
52
|
+
|
53
|
+
<form action="list" method="post">
|
54
|
+
|
55
|
+
<select name="ku">
|
56
|
+
|
57
|
+
<%if (list != null ) { %>
|
58
|
+
|
59
|
+
<% for (int i = 0; i < list.size(); i++) { %>
|
60
|
+
|
61
|
+
<%KuyakushoBean KuyakushoBean= (KuyakushoBean)list.get(i);%>
|
62
|
+
|
63
|
+
<option value=<%=KuyakushoBean.getid() %>><%=KuyakushoBean.getku() %></option>
|
64
|
+
|
65
|
+
<% } %>
|
66
|
+
|
67
|
+
<% } %>
|
68
|
+
|
69
|
+
//追加した部分
|
70
|
+
|
71
|
+
<%if (name != null ) { %>
|
72
|
+
|
73
|
+
<% for (int i = 0; i < name.size(); i++) { %>
|
74
|
+
|
75
|
+
<%KuyakushoBean KuyakushoBean= (KuyakushoBean)name.get(i);%>
|
76
|
+
|
77
|
+
<option value=<%=KuyakushoBean.getid() %>selected><%=KuyakushoBean.getku() %></option>
|
78
|
+
|
79
|
+
<% } %>
|
80
|
+
|
81
|
+
<% } %>
|
82
|
+
|
83
|
+
</select> <input type="submit" value="送信">
|
84
|
+
|
85
|
+
</form>
|
86
|
+
|
87
|
+
<table border="1">
|
88
|
+
|
89
|
+
<tr>
|
90
|
+
|
91
|
+
<th>番号</th>
|
92
|
+
|
93
|
+
<th>区</th>
|
94
|
+
|
95
|
+
<th>郵便番号</th>
|
96
|
+
|
97
|
+
<th>住所</th>
|
98
|
+
|
99
|
+
<th>電話番号</th>
|
100
|
+
|
101
|
+
</tr>
|
102
|
+
|
103
|
+
<%if (list != null ) { %>
|
104
|
+
|
105
|
+
<% for (int j = 0; j < list.size(); j++) { %>
|
106
|
+
|
107
|
+
<%KuyakushoBean KuyakushoBean= (KuyakushoBean)list.get(j);%>
|
108
|
+
|
109
|
+
<tr>
|
110
|
+
|
111
|
+
<td><%=KuyakushoBean.getid()%></td>
|
112
|
+
|
113
|
+
<td><%=KuyakushoBean.getku()%></td>
|
114
|
+
|
115
|
+
<td><%=KuyakushoBean.getzip()%></td>
|
116
|
+
|
117
|
+
<td><%=KuyakushoBean.getaddress()%></td>
|
118
|
+
|
119
|
+
<td><%=KuyakushoBean.gettel()%></td>
|
120
|
+
|
121
|
+
</tr>
|
122
|
+
|
123
|
+
<% } %>
|
124
|
+
|
125
|
+
<% } %>
|
126
|
+
|
127
|
+
<%if(name != null) {%>
|
128
|
+
|
129
|
+
<% for (int i = 0; i < name.size(); i++) { %>
|
130
|
+
|
131
|
+
<%KuyakushoBean KuyakushoBean= (KuyakushoBean)name.get(i);%>
|
132
|
+
|
133
|
+
<tr>
|
134
|
+
|
135
|
+
<td><%=KuyakushoBean.getid()%></td>
|
136
|
+
|
137
|
+
<td><%=KuyakushoBean.getku()%></td>
|
138
|
+
|
139
|
+
<td><%=KuyakushoBean.getzip()%></td>
|
140
|
+
|
141
|
+
<td><%=KuyakushoBean.getaddress()%></td>
|
142
|
+
|
143
|
+
<td><%=KuyakushoBean.gettel()%></td>
|
144
|
+
|
145
|
+
</tr>
|
146
|
+
|
147
|
+
<% } %>
|
148
|
+
|
149
|
+
<% } %>
|
150
|
+
|
151
|
+
</table>
|
152
|
+
|
153
|
+
<input type="hidden" name="ku2">
|
154
|
+
|
155
|
+
</div>
|
156
|
+
|
157
|
+
</body>
|
158
|
+
|
159
|
+
</html>
|
160
|
+
|
161
|
+
```
|
162
|
+
|
163
|
+
###サーブレット
|
164
|
+
|
165
|
+
```java
|
166
|
+
|
167
|
+
package jp.co.keyaki.controller;
|
168
|
+
|
169
|
+
import java.io.IOException;
|
170
|
+
|
171
|
+
import java.util.List;
|
172
|
+
|
173
|
+
import javax.servlet.RequestDispatcher;
|
174
|
+
|
175
|
+
import javax.servlet.ServletContext;
|
176
|
+
|
177
|
+
import javax.servlet.ServletException;
|
178
|
+
|
179
|
+
import javax.servlet.http.HttpServlet;
|
180
|
+
|
181
|
+
import javax.servlet.http.HttpServletRequest;
|
182
|
+
|
183
|
+
import javax.servlet.http.HttpServletResponse;
|
184
|
+
|
185
|
+
import jp.co.keyaki.bean.KuyakushoBean;
|
186
|
+
|
187
|
+
import jp.co.keyaki.service.KuyakushoSearch;
|
188
|
+
|
189
|
+
public class KuyakushoController extends HttpServlet {
|
190
|
+
|
191
|
+
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
192
|
+
|
193
|
+
throws ServletException, IOException {
|
194
|
+
|
195
|
+
String target = null;
|
196
|
+
|
197
|
+
try {
|
198
|
+
|
199
|
+
KuyakushoSearch kuyakushoSearch = new KuyakushoSearch();
|
200
|
+
|
201
|
+
List<KuyakushoBean> list = kuyakushoSearch.list();
|
202
|
+
|
203
|
+
request.setAttribute("LIST", list);
|
204
|
+
|
205
|
+
target = "/kuyakusho.jsp";
|
206
|
+
|
207
|
+
} catch (Exception e) {
|
208
|
+
|
209
|
+
e.printStackTrace();
|
210
|
+
|
211
|
+
target = "/systemError.jsp";
|
212
|
+
|
213
|
+
} finally {
|
214
|
+
|
215
|
+
ServletContext context = this.getServletContext();
|
216
|
+
|
217
|
+
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
|
218
|
+
|
219
|
+
dispatcher.forward(request, response);
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
226
|
+
|
227
|
+
throws ServletException, IOException {
|
228
|
+
|
229
|
+
String target = null;
|
230
|
+
|
231
|
+
try{
|
232
|
+
|
233
|
+
int id = Integer.parseInt(request.getParameter("ku"));
|
234
|
+
|
235
|
+
KuyakushoSearch kuyakushoSearch = new KuyakushoSearch();
|
236
|
+
|
237
|
+
List<KuyakushoBean> name = kuyakushoSearch.name(id);
|
238
|
+
|
239
|
+
request.setAttribute("ku", name);
|
240
|
+
|
241
|
+
if(name == null) {
|
242
|
+
|
243
|
+
request.setAttribute("ku", name);
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
target = "/kuyakusho.jsp";
|
248
|
+
|
249
|
+
} catch (Exception e) {
|
250
|
+
|
251
|
+
e.printStackTrace();
|
252
|
+
|
253
|
+
target = "/systemError.jsp";
|
254
|
+
|
255
|
+
} finally {
|
256
|
+
|
257
|
+
ServletContext context = this.getServletContext();
|
258
|
+
|
259
|
+
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
|
260
|
+
|
261
|
+
dispatcher.forward(request, response);
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
```
|
270
|
+
|
271
|
+
###検索処理
|
272
|
+
|
273
|
+
```java
|
274
|
+
|
275
|
+
package jp.co.keyaki.service;
|
276
|
+
|
277
|
+
import java.sql.Connection;
|
278
|
+
|
279
|
+
import java.sql.DriverManager;
|
280
|
+
|
281
|
+
import java.sql.PreparedStatement;
|
282
|
+
|
283
|
+
import java.sql.ResultSet;
|
284
|
+
|
285
|
+
import java.sql.SQLException;
|
286
|
+
|
287
|
+
import java.util.ArrayList;
|
288
|
+
|
289
|
+
import java.util.List;
|
290
|
+
|
291
|
+
import org.postgresql.util.PSQLException;
|
292
|
+
|
293
|
+
import jp.co.keyaki.bean.KuyakushoBean;
|
294
|
+
|
295
|
+
public class KuyakushoSearch {
|
296
|
+
|
297
|
+
public List<KuyakushoBean> list()
|
298
|
+
|
299
|
+
throws ClassNotFoundException, SQLException {
|
300
|
+
|
301
|
+
List<KuyakushoBean> list = new ArrayList<>();
|
302
|
+
|
303
|
+
Connection connection = null;
|
304
|
+
|
305
|
+
PreparedStatement preparedStatement = null;
|
306
|
+
|
307
|
+
ResultSet resultSet = null;
|
308
|
+
|
309
|
+
KuyakushoBean KuyakushoBean = null;
|
310
|
+
|
311
|
+
try {
|
312
|
+
|
313
|
+
Class.forName("org.postgresql.Driver");
|
314
|
+
|
315
|
+
connection = DriverManager.getConnection(
|
316
|
+
|
317
|
+
jdbc:postgresql://【ホスト名】/【データベース名】", "【ユーザ名】", "【パスワード】");
|
318
|
+
|
319
|
+
String SQL = "SELECT id, ku, zip, address, tel FROM t_kuyakusho";
|
320
|
+
|
321
|
+
preparedStatement = connection.prepareStatement(SQL);
|
322
|
+
|
323
|
+
ResultSet rs = preparedStatement.executeQuery();
|
324
|
+
|
325
|
+
while (rs.next()) {
|
326
|
+
|
327
|
+
KuyakushoBean = new KuyakushoBean();
|
328
|
+
|
329
|
+
KuyakushoBean.setid(rs.getInt("id"));
|
330
|
+
|
331
|
+
KuyakushoBean.setku(rs.getString("ku"));
|
332
|
+
|
333
|
+
KuyakushoBean.setzip(rs.getString("zip"));
|
334
|
+
|
335
|
+
KuyakushoBean.setaddress(rs.getString("address"));
|
336
|
+
|
337
|
+
KuyakushoBean.settel(rs.getString("tel"));
|
338
|
+
|
339
|
+
list.add(KuyakushoBean);
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
} catch (ClassNotFoundException e) {
|
344
|
+
|
345
|
+
e.printStackTrace();
|
346
|
+
|
347
|
+
} catch (PSQLException e) {
|
348
|
+
|
349
|
+
e.printStackTrace();
|
350
|
+
|
351
|
+
} finally {
|
352
|
+
|
353
|
+
if (resultSet != null) {
|
354
|
+
|
355
|
+
resultSet.close();
|
356
|
+
|
357
|
+
}
|
358
|
+
|
359
|
+
if (preparedStatement != null) {
|
360
|
+
|
361
|
+
preparedStatement.close();
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
if (connection != null) {
|
366
|
+
|
367
|
+
connection.close();
|
368
|
+
|
369
|
+
}
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
return list;
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
public List<KuyakushoBean> name(int id)
|
378
|
+
|
379
|
+
throws ClassNotFoundException, SQLException {
|
380
|
+
|
381
|
+
List<KuyakushoBean> name = new ArrayList<>();
|
382
|
+
|
383
|
+
Connection connection = null;
|
384
|
+
|
385
|
+
PreparedStatement preparedStatement = null;
|
386
|
+
|
387
|
+
ResultSet resultSet = null;
|
388
|
+
|
389
|
+
KuyakushoBean KuyakushoBean = null;
|
390
|
+
|
391
|
+
try {
|
392
|
+
|
393
|
+
Class.forName("org.postgresql.Driver");
|
394
|
+
|
395
|
+
connection = DriverManager.getConnection(
|
396
|
+
|
397
|
+
"jdbc:postgresql://localhost:5432/lessondb", "postgres", "jobsupport");
|
398
|
+
|
399
|
+
String SQL = "SELECT id, ku, zip, address, tel FROM t_kuyakusho WHERE id = ?";
|
400
|
+
|
401
|
+
preparedStatement = connection.prepareStatement(SQL);
|
402
|
+
|
403
|
+
preparedStatement.setInt(1, id);
|
404
|
+
|
405
|
+
ResultSet rs = preparedStatement.executeQuery();
|
406
|
+
|
407
|
+
if (rs.next()) {
|
408
|
+
|
409
|
+
KuyakushoBean = new KuyakushoBean();
|
410
|
+
|
411
|
+
KuyakushoBean.setid(rs.getInt("id"));
|
412
|
+
|
413
|
+
KuyakushoBean.setku(rs.getString("ku"));
|
414
|
+
|
415
|
+
KuyakushoBean.setzip(rs.getString("zip"));
|
416
|
+
|
417
|
+
KuyakushoBean.setaddress(rs.getString("address"));
|
418
|
+
|
419
|
+
KuyakushoBean.settel(rs.getString("tel"));
|
420
|
+
|
421
|
+
name.add(KuyakushoBean);
|
422
|
+
|
423
|
+
}
|
424
|
+
|
425
|
+
} catch (ClassNotFoundException e) {
|
426
|
+
|
427
|
+
e.printStackTrace();
|
428
|
+
|
429
|
+
} catch (PSQLException e) {
|
430
|
+
|
431
|
+
e.printStackTrace();
|
432
|
+
|
433
|
+
} finally {
|
434
|
+
|
435
|
+
if (resultSet != null) {
|
436
|
+
|
437
|
+
resultSet.close();
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
if (preparedStatement != null) {
|
442
|
+
|
443
|
+
preparedStatement.close();
|
444
|
+
|
445
|
+
}
|
446
|
+
|
447
|
+
if (connection != null) {
|
448
|
+
|
449
|
+
connection.close();
|
450
|
+
|
451
|
+
}
|
452
|
+
|
453
|
+
}
|
454
|
+
|
455
|
+
return name;
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
}
|
460
|
+
|
461
|
+
```
|
462
|
+
|
463
|
+
###Bean
|
464
|
+
|
465
|
+
```java
|
466
|
+
|
467
|
+
package jp.co.keyaki.bean;
|
468
|
+
|
469
|
+
public class KuyakushoBean {
|
470
|
+
|
471
|
+
private int id;
|
472
|
+
|
473
|
+
private String ku;
|
474
|
+
|
475
|
+
private String zip;
|
476
|
+
|
477
|
+
private String address;
|
478
|
+
|
479
|
+
private String tel;
|
480
|
+
|
481
|
+
public void setid(int id) {
|
482
|
+
|
483
|
+
this.id = id;
|
484
|
+
|
485
|
+
}
|
486
|
+
|
487
|
+
public int getid() {
|
488
|
+
|
489
|
+
return id;
|
490
|
+
|
491
|
+
}
|
492
|
+
|
493
|
+
public void setku(String ku) {
|
494
|
+
|
495
|
+
this.ku = ku;
|
496
|
+
|
497
|
+
}
|
498
|
+
|
499
|
+
public String getku() {
|
500
|
+
|
501
|
+
return ku;
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
public void setzip(String zip) {
|
506
|
+
|
507
|
+
this.zip = zip;
|
508
|
+
|
509
|
+
}
|
510
|
+
|
511
|
+
public String getzip() {
|
512
|
+
|
513
|
+
return zip;
|
514
|
+
|
515
|
+
}
|
516
|
+
|
517
|
+
public void setaddress(String address) {
|
518
|
+
|
519
|
+
this.address = address;
|
520
|
+
|
521
|
+
}
|
522
|
+
|
523
|
+
public String getaddress() {
|
524
|
+
|
525
|
+
return address;
|
526
|
+
|
527
|
+
}
|
528
|
+
|
529
|
+
public void settel(String tel) {
|
530
|
+
|
531
|
+
this.tel = tel;
|
532
|
+
|
533
|
+
}
|
534
|
+
|
535
|
+
public String gettel() {
|
536
|
+
|
537
|
+
return tel;
|
538
|
+
|
539
|
+
}
|
540
|
+
|
541
|
+
}
|
542
|
+
|
543
|
+
```
|
1
削除
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
削、、、、、、、、、、、、、、、、、、、、
|
test
CHANGED
@@ -1,647 +1 @@
|
|
1
|
-
いつもお世話になっております。
|
2
|
-
|
3
|
-
プルダウンで項目名を選択後にボタンのクリックで送信し、その項目名に合致する情報を表示するプログラムを作成しております。
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
送信後のプルダウンを、選択した項目が初期状態になり、他の項目名もプルダウンにて確認できるようにしたいです。
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
追加したコードで初期値を設定したのですが、これではプルダウンに1件しか表示されないため、初期値を設定しつつ他の項目をプルダウン内に表示したいのですが、
|
12
|
-
|
13
|
-
どのように修正、記述をすればよいのかが分からないためご教示いただければ幸いです。
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
よろしくお願いいたします。
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
###表示画面
|
32
|
-
|
33
|
-
```jsp
|
34
|
-
|
35
|
-
<%@ page contentType="text/html; charset=Windows-31J"%>
|
36
|
-
|
37
|
-
<%@ page import="jp.co.keyaki.bean.KuyakushoBean"%>
|
38
|
-
|
39
|
-
<%@ page import="java.util.List"%>
|
40
|
-
|
41
|
-
<%@ page import="java.util.ArrayList"%>
|
42
|
-
|
43
|
-
<% @SuppressWarnings("unchecked")
|
44
|
-
|
45
|
-
List<KuyakushoBean> list = (List<KuyakushoBean>) request.getAttribute("LIST");
|
46
|
-
|
47
|
-
%>
|
48
|
-
|
49
|
-
<% @SuppressWarnings("unchecked")
|
50
|
-
|
51
|
-
List<KuyakushoBean> name = (List<KuyakushoBean>) request.getAttribute("ku");
|
52
|
-
|
53
|
-
%>
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
<html>
|
58
|
-
|
59
|
-
<head>
|
60
|
-
|
61
|
-
<meta charset="UTF-8">
|
62
|
-
|
63
|
-
<title>23区区役所一覧</title>
|
64
|
-
|
65
|
-
</head>
|
66
|
-
|
67
|
-
<body>
|
68
|
-
|
69
|
-
|
1
|
+
削除。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
|
70
|
-
|
71
|
-
23区区役所一覧
|
72
|
-
|
73
|
-
<form action="list" method="post">
|
74
|
-
|
75
|
-
<select name="ku">
|
76
|
-
|
77
|
-
<%if (list != null ) { %>
|
78
|
-
|
79
|
-
<% for (int i = 0; i < list.size(); i++) { %>
|
80
|
-
|
81
|
-
<%KuyakushoBean KuyakushoBean= (KuyakushoBean)list.get(i);%>
|
82
|
-
|
83
|
-
<option value=<%=KuyakushoBean.getid() %>><%=KuyakushoBean.getku() %></option>
|
84
|
-
|
85
|
-
<% } %>
|
86
|
-
|
87
|
-
<% } %>
|
88
|
-
|
89
|
-
//追加した部分
|
90
|
-
|
91
|
-
<%if (name != null ) { %>
|
92
|
-
|
93
|
-
<% for (int i = 0; i < name.size(); i++) { %>
|
94
|
-
|
95
|
-
<%KuyakushoBean KuyakushoBean= (KuyakushoBean)name.get(i);%>
|
96
|
-
|
97
|
-
<option value=<%=KuyakushoBean.getid() %>selected><%=KuyakushoBean.getku() %></option>
|
98
|
-
|
99
|
-
<% } %>
|
100
|
-
|
101
|
-
<% } %>
|
102
|
-
|
103
|
-
</select> <input type="submit" value="送信">
|
104
|
-
|
105
|
-
</form>
|
106
|
-
|
107
|
-
<table border="1">
|
108
|
-
|
109
|
-
<tr>
|
110
|
-
|
111
|
-
<th>番号</th>
|
112
|
-
|
113
|
-
<th>区</th>
|
114
|
-
|
115
|
-
<th>郵便番号</th>
|
116
|
-
|
117
|
-
<th>住所</th>
|
118
|
-
|
119
|
-
<th>電話番号</th>
|
120
|
-
|
121
|
-
</tr>
|
122
|
-
|
123
|
-
<%if (list != null ) { %>
|
124
|
-
|
125
|
-
<% for (int j = 0; j < list.size(); j++) { %>
|
126
|
-
|
127
|
-
<%KuyakushoBean KuyakushoBean= (KuyakushoBean)list.get(j);%>
|
128
|
-
|
129
|
-
<tr>
|
130
|
-
|
131
|
-
<td><%=KuyakushoBean.getid()%></td>
|
132
|
-
|
133
|
-
<td><%=KuyakushoBean.getku()%></td>
|
134
|
-
|
135
|
-
<td><%=KuyakushoBean.getzip()%></td>
|
136
|
-
|
137
|
-
<td><%=KuyakushoBean.getaddress()%></td>
|
138
|
-
|
139
|
-
<td><%=KuyakushoBean.gettel()%></td>
|
140
|
-
|
141
|
-
</tr>
|
142
|
-
|
143
|
-
<% } %>
|
144
|
-
|
145
|
-
<% } %>
|
146
|
-
|
147
|
-
<%if(name != null) {%>
|
148
|
-
|
149
|
-
<% for (int i = 0; i < name.size(); i++) { %>
|
150
|
-
|
151
|
-
<%KuyakushoBean KuyakushoBean= (KuyakushoBean)name.get(i);%>
|
152
|
-
|
153
|
-
<tr>
|
154
|
-
|
155
|
-
<td><%=KuyakushoBean.getid()%></td>
|
156
|
-
|
157
|
-
<td><%=KuyakushoBean.getku()%></td>
|
158
|
-
|
159
|
-
<td><%=KuyakushoBean.getzip()%></td>
|
160
|
-
|
161
|
-
<td><%=KuyakushoBean.getaddress()%></td>
|
162
|
-
|
163
|
-
<td><%=KuyakushoBean.gettel()%></td>
|
164
|
-
|
165
|
-
</tr>
|
166
|
-
|
167
|
-
<% } %>
|
168
|
-
|
169
|
-
<% } %>
|
170
|
-
|
171
|
-
</table>
|
172
|
-
|
173
|
-
<input type="hidden" name="ku2">
|
174
|
-
|
175
|
-
</div>
|
176
|
-
|
177
|
-
</body>
|
178
|
-
|
179
|
-
</html>
|
180
|
-
|
181
|
-
```
|
182
|
-
|
183
|
-
###サーブレット
|
184
|
-
|
185
|
-
```java
|
186
|
-
|
187
|
-
package jp.co.keyaki.controller;
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
import java.io.IOException;
|
192
|
-
|
193
|
-
import java.util.List;
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
import javax.servlet.RequestDispatcher;
|
198
|
-
|
199
|
-
import javax.servlet.ServletContext;
|
200
|
-
|
201
|
-
import javax.servlet.ServletException;
|
202
|
-
|
203
|
-
import javax.servlet.http.HttpServlet;
|
204
|
-
|
205
|
-
import javax.servlet.http.HttpServletRequest;
|
206
|
-
|
207
|
-
import javax.servlet.http.HttpServletResponse;
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
import jp.co.keyaki.bean.KuyakushoBean;
|
212
|
-
|
213
|
-
import jp.co.keyaki.service.KuyakushoSearch;
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
public class KuyakushoController extends HttpServlet {
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
222
|
-
|
223
|
-
throws ServletException, IOException {
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
String target = null;
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
try {
|
232
|
-
|
233
|
-
KuyakushoSearch kuyakushoSearch = new KuyakushoSearch();
|
234
|
-
|
235
|
-
List<KuyakushoBean> list = kuyakushoSearch.list();
|
236
|
-
|
237
|
-
request.setAttribute("LIST", list);
|
238
|
-
|
239
|
-
target = "/kuyakusho.jsp";
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
} catch (Exception e) {
|
244
|
-
|
245
|
-
e.printStackTrace();
|
246
|
-
|
247
|
-
target = "/systemError.jsp";
|
248
|
-
|
249
|
-
} finally {
|
250
|
-
|
251
|
-
ServletContext context = this.getServletContext();
|
252
|
-
|
253
|
-
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
|
254
|
-
|
255
|
-
dispatcher.forward(request, response);
|
256
|
-
|
257
|
-
}
|
258
|
-
|
259
|
-
}
|
260
|
-
|
261
|
-
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
262
|
-
|
263
|
-
throws ServletException, IOException {
|
264
|
-
|
265
|
-
String target = null;
|
266
|
-
|
267
|
-
try{
|
268
|
-
|
269
|
-
int id = Integer.parseInt(request.getParameter("ku"));
|
270
|
-
|
271
|
-
KuyakushoSearch kuyakushoSearch = new KuyakushoSearch();
|
272
|
-
|
273
|
-
List<KuyakushoBean> name = kuyakushoSearch.name(id);
|
274
|
-
|
275
|
-
request.setAttribute("ku", name);
|
276
|
-
|
277
|
-
if(name == null) {
|
278
|
-
|
279
|
-
request.setAttribute("ku", name);
|
280
|
-
|
281
|
-
}
|
282
|
-
|
283
|
-
target = "/kuyakusho.jsp";
|
284
|
-
|
285
|
-
} catch (Exception e) {
|
286
|
-
|
287
|
-
e.printStackTrace();
|
288
|
-
|
289
|
-
target = "/systemError.jsp";
|
290
|
-
|
291
|
-
} finally {
|
292
|
-
|
293
|
-
ServletContext context = this.getServletContext();
|
294
|
-
|
295
|
-
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
|
296
|
-
|
297
|
-
dispatcher.forward(request, response);
|
298
|
-
|
299
|
-
}
|
300
|
-
|
301
|
-
}
|
302
|
-
|
303
|
-
}
|
304
|
-
|
305
|
-
```
|
306
|
-
|
307
|
-
###検索処理
|
308
|
-
|
309
|
-
```java
|
310
|
-
|
311
|
-
package jp.co.keyaki.service;
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
import java.sql.Connection;
|
316
|
-
|
317
|
-
import java.sql.DriverManager;
|
318
|
-
|
319
|
-
import java.sql.PreparedStatement;
|
320
|
-
|
321
|
-
import java.sql.ResultSet;
|
322
|
-
|
323
|
-
import java.sql.SQLException;
|
324
|
-
|
325
|
-
import java.util.ArrayList;
|
326
|
-
|
327
|
-
import java.util.List;
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
import org.postgresql.util.PSQLException;
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
import jp.co.keyaki.bean.KuyakushoBean;
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
public class KuyakushoSearch {
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
public List<KuyakushoBean> list()
|
344
|
-
|
345
|
-
throws ClassNotFoundException, SQLException {
|
346
|
-
|
347
|
-
List<KuyakushoBean> list = new ArrayList<>();
|
348
|
-
|
349
|
-
Connection connection = null;
|
350
|
-
|
351
|
-
PreparedStatement preparedStatement = null;
|
352
|
-
|
353
|
-
ResultSet resultSet = null;
|
354
|
-
|
355
|
-
KuyakushoBean KuyakushoBean = null;
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
try {
|
360
|
-
|
361
|
-
Class.forName("org.postgresql.Driver");
|
362
|
-
|
363
|
-
connection = DriverManager.getConnection(
|
364
|
-
|
365
|
-
jdbc:postgresql://【ホスト名】/【データベース名】", "【ユーザ名】", "【パスワード】");
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
String SQL = "SELECT id, ku, zip, address, tel FROM t_kuyakusho";
|
370
|
-
|
371
|
-
preparedStatement = connection.prepareStatement(SQL);
|
372
|
-
|
373
|
-
ResultSet rs = preparedStatement.executeQuery();
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
while (rs.next()) {
|
378
|
-
|
379
|
-
KuyakushoBean = new KuyakushoBean();
|
380
|
-
|
381
|
-
KuyakushoBean.setid(rs.getInt("id"));
|
382
|
-
|
383
|
-
KuyakushoBean.setku(rs.getString("ku"));
|
384
|
-
|
385
|
-
KuyakushoBean.setzip(rs.getString("zip"));
|
386
|
-
|
387
|
-
KuyakushoBean.setaddress(rs.getString("address"));
|
388
|
-
|
389
|
-
KuyakushoBean.settel(rs.getString("tel"));
|
390
|
-
|
391
|
-
list.add(KuyakushoBean);
|
392
|
-
|
393
|
-
}
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
} catch (ClassNotFoundException e) {
|
398
|
-
|
399
|
-
e.printStackTrace();
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
} catch (PSQLException e) {
|
404
|
-
|
405
|
-
e.printStackTrace();
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
} finally {
|
410
|
-
|
411
|
-
if (resultSet != null) {
|
412
|
-
|
413
|
-
resultSet.close();
|
414
|
-
|
415
|
-
}
|
416
|
-
|
417
|
-
if (preparedStatement != null) {
|
418
|
-
|
419
|
-
preparedStatement.close();
|
420
|
-
|
421
|
-
}
|
422
|
-
|
423
|
-
if (connection != null) {
|
424
|
-
|
425
|
-
connection.close();
|
426
|
-
|
427
|
-
}
|
428
|
-
|
429
|
-
}
|
430
|
-
|
431
|
-
return list;
|
432
|
-
|
433
|
-
}
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
public List<KuyakushoBean> name(int id)
|
440
|
-
|
441
|
-
throws ClassNotFoundException, SQLException {
|
442
|
-
|
443
|
-
List<KuyakushoBean> name = new ArrayList<>();
|
444
|
-
|
445
|
-
Connection connection = null;
|
446
|
-
|
447
|
-
PreparedStatement preparedStatement = null;
|
448
|
-
|
449
|
-
ResultSet resultSet = null;
|
450
|
-
|
451
|
-
KuyakushoBean KuyakushoBean = null;
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
try {
|
456
|
-
|
457
|
-
Class.forName("org.postgresql.Driver");
|
458
|
-
|
459
|
-
connection = DriverManager.getConnection(
|
460
|
-
|
461
|
-
"jdbc:postgresql://localhost:5432/lessondb", "postgres", "jobsupport");
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
String SQL = "SELECT id, ku, zip, address, tel FROM t_kuyakusho WHERE id = ?";
|
466
|
-
|
467
|
-
preparedStatement = connection.prepareStatement(SQL);
|
468
|
-
|
469
|
-
preparedStatement.setInt(1, id);
|
470
|
-
|
471
|
-
ResultSet rs = preparedStatement.executeQuery();
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
if (rs.next()) {
|
476
|
-
|
477
|
-
KuyakushoBean = new KuyakushoBean();
|
478
|
-
|
479
|
-
KuyakushoBean.setid(rs.getInt("id"));
|
480
|
-
|
481
|
-
KuyakushoBean.setku(rs.getString("ku"));
|
482
|
-
|
483
|
-
KuyakushoBean.setzip(rs.getString("zip"));
|
484
|
-
|
485
|
-
KuyakushoBean.setaddress(rs.getString("address"));
|
486
|
-
|
487
|
-
KuyakushoBean.settel(rs.getString("tel"));
|
488
|
-
|
489
|
-
name.add(KuyakushoBean);
|
490
|
-
|
491
|
-
}
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
} catch (ClassNotFoundException e) {
|
496
|
-
|
497
|
-
e.printStackTrace();
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
} catch (PSQLException e) {
|
502
|
-
|
503
|
-
e.printStackTrace();
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
} finally {
|
508
|
-
|
509
|
-
if (resultSet != null) {
|
510
|
-
|
511
|
-
resultSet.close();
|
512
|
-
|
513
|
-
}
|
514
|
-
|
515
|
-
if (preparedStatement != null) {
|
516
|
-
|
517
|
-
preparedStatement.close();
|
518
|
-
|
519
|
-
}
|
520
|
-
|
521
|
-
if (connection != null) {
|
522
|
-
|
523
|
-
connection.close();
|
524
|
-
|
525
|
-
}
|
526
|
-
|
527
|
-
}
|
528
|
-
|
529
|
-
return name;
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
}
|
534
|
-
|
535
|
-
}
|
536
|
-
|
537
|
-
```
|
538
|
-
|
539
|
-
###Bean
|
540
|
-
|
541
|
-
```java
|
542
|
-
|
543
|
-
package jp.co.keyaki.bean;
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
public class KuyakushoBean {
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
private int id;
|
552
|
-
|
553
|
-
private String ku;
|
554
|
-
|
555
|
-
private String zip;
|
556
|
-
|
557
|
-
private String address;
|
558
|
-
|
559
|
-
private String tel;
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
public void setid(int id) {
|
566
|
-
|
567
|
-
this.id = id;
|
568
|
-
|
569
|
-
}
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
public int getid() {
|
574
|
-
|
575
|
-
return id;
|
576
|
-
|
577
|
-
}
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
public void setku(String ku) {
|
582
|
-
|
583
|
-
this.ku = ku;
|
584
|
-
|
585
|
-
}
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
public String getku() {
|
590
|
-
|
591
|
-
return ku;
|
592
|
-
|
593
|
-
}
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
public void setzip(String zip) {
|
598
|
-
|
599
|
-
this.zip = zip;
|
600
|
-
|
601
|
-
}
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
public String getzip() {
|
606
|
-
|
607
|
-
return zip;
|
608
|
-
|
609
|
-
}
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
public void setaddress(String address) {
|
614
|
-
|
615
|
-
this.address = address;
|
616
|
-
|
617
|
-
}
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
public String getaddress() {
|
622
|
-
|
623
|
-
return address;
|
624
|
-
|
625
|
-
}
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
public void settel(String tel) {
|
630
|
-
|
631
|
-
this.tel = tel;
|
632
|
-
|
633
|
-
}
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
public String gettel() {
|
638
|
-
|
639
|
-
return tel;
|
640
|
-
|
641
|
-
}
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
}
|
646
|
-
|
647
|
-
```
|