質問編集履歴

1

コードの追加

2020/09/16 03:57

投稿

Y.Mamoru
Y.Mamoru

スコア47

test CHANGED
File without changes
test CHANGED
@@ -73,3 +73,507 @@
73
73
  ずれた考え方をしてしまっている気がするのですが、
74
74
 
75
75
  アドバイスを言お願いします。
76
+
77
+
78
+
79
+ 以下全体のコードです。
80
+
81
+ ```jsp
82
+
83
+ <%@ page language="java" contentType="text/html; charset=UTF-8"
84
+
85
+ pageEncoding="UTF-8"%>
86
+
87
+ <%@ page import="Change.DataBase"%>
88
+
89
+ <%@ page import="java.lang.*" %>
90
+
91
+ <!DOCTYPE html>
92
+
93
+ <html>
94
+
95
+ <head>
96
+
97
+ <meta charset="UTF-8">
98
+
99
+ <title>Change_task</title>
100
+
101
+ <link rel="stylesheet" href="../CSS/Change.css">
102
+
103
+ <SCRIPT type="text/javascript" src="../js/Check_Change.js"></SCRIPT>
104
+
105
+ </head>
106
+
107
+ <body>
108
+
109
+ <H1>アンケートフォーム</H1>
110
+
111
+ <% DataBase db = new DataBase(); %>
112
+
113
+ <%
114
+
115
+ if(db.getError() != null){ %>
116
+
117
+
118
+
119
+ <p><%= System.out.println(db.getError()) %></p>
120
+
121
+
122
+
123
+ <%
124
+
125
+ }else{
126
+
127
+ %>
128
+
129
+ <P>以下のアンケートフォームを入力して変更ボタンを押下してください</P>
130
+
131
+ <FORM method="POST" name="question_form" id="form" action="/Change/ChangeServlet">
132
+
133
+ <Input type="hidden" name="hiddenNO" value=<%= request.getParameter("ankeno") %>>
134
+
135
+ <input type="hidden" name="move" value="update">
136
+
137
+ <TABLE class="table">
138
+
139
+ <TR class="th1">
140
+
141
+ <TH name="left-table">質問</TH>
142
+
143
+ <TH>回答</TH>
144
+
145
+ </TR>
146
+
147
+
148
+
149
+ ~中略~
150
+
151
+ </TABLE>
152
+
153
+ <DIV class="send_div">
154
+
155
+ <INPUT type="submit" class="send_button" value="変更" >
156
+
157
+ </DIV>
158
+
159
+ </FORM>
160
+
161
+ <FORM>
162
+
163
+ <DIV>
164
+
165
+ <INPUT type="button" class="reset" value="リセット" onClick="location.reload();">
166
+
167
+ </DIV>
168
+
169
+ </FORM>
170
+
171
+ <% } %>
172
+
173
+ </body>
174
+
175
+ </html>
176
+
177
+ ```
178
+
179
+
180
+
181
+ ```servlet
182
+
183
+ package Change;
184
+
185
+
186
+
187
+ import java.io.IOException;
188
+
189
+ import java.sql.Connection;
190
+
191
+ import java.sql.PreparedStatement;
192
+
193
+ import java.sql.SQLException;
194
+
195
+ import java.sql.Statement;
196
+
197
+
198
+
199
+ import javax.servlet.ServletException;
200
+
201
+ import javax.servlet.http.HttpServlet;
202
+
203
+ import javax.servlet.http.HttpServletRequest;
204
+
205
+ import javax.servlet.http.HttpServletResponse;
206
+
207
+
208
+
209
+
210
+
211
+ public class ChangeServlet extends HttpServlet {
212
+
213
+ private static final long serialVersionUID = 1L;
214
+
215
+
216
+
217
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
218
+
219
+ request.setCharacterEncoding("utf-8");
220
+
221
+
222
+
223
+ //前準備
224
+
225
+ String e;
226
+
227
+ int count = 0;
228
+
229
+ DataBase db = new DataBase();
230
+
231
+
232
+
233
+ //各データを受け取って、条件に合うのか確かめる
234
+
235
+ //郵便番号
236
+
237
+ String p1 = request.getParameter("post_num1");
238
+
239
+ String p2 = request.getParameter("post_num2");
240
+
241
+ if(p1 != null && p1.length() ==3) { //郵便番号1がNullではなくかつ3桁か
242
+
243
+ count++;
244
+
245
+ }else {
246
+
247
+ e = "郵便番号1は3桁の数字で入力してください";
248
+
249
+ db.setError(e);
250
+
251
+ request.getRequestDispatcher("/jsp/Change_Update.jsp").forward(request, response);
252
+
253
+ }
254
+
255
+ if(p2 != null && p2.length() ==4) { //郵便番号2がNullではなくかつ4桁か
256
+
257
+ count++;
258
+
259
+ }else {
260
+
261
+ e = "郵便番号2は4桁の数字で入力してください";
262
+
263
+ db.setError(e);
264
+
265
+ request.getRequestDispatcher("/jsp/Change_Update.jsp").forward(request, response);
266
+
267
+ }
268
+
269
+ //住所チェック
270
+
271
+ String address = request.getParameter("address");
272
+
273
+ if(address != null) {
274
+
275
+ count++;
276
+
277
+ }else {
278
+
279
+ e = "住所を入力してください";
280
+
281
+ db.setError(e);
282
+
283
+ request.getRequestDispatcher("/jsp/Change_Update.jsp").forward(request, response);
284
+
285
+ }
286
+
287
+ //氏名
288
+
289
+ String name = request.getParameter("name");
290
+
291
+ if(name != null ) {
292
+
293
+ count++;
294
+
295
+ }else {
296
+
297
+ e = "氏名を入力してください";
298
+
299
+ db.setError(e);
300
+
301
+ request.getRequestDispatcher("/jsp/Change_Update.jsp").forward(request, response);
302
+
303
+ }
304
+
305
+ //年
306
+
307
+ String year = request.getParameter("year");
308
+
309
+ if(year != null) {
310
+
311
+ count++;
312
+
313
+ }else {
314
+
315
+ e = "生まれ年を入力してください";
316
+
317
+ db.setError(e);
318
+
319
+ request.getRequestDispatcher("/jsp/Change_Update.jsp").forward(request, response);
320
+
321
+ }
322
+
323
+ //月
324
+
325
+ String month = request.getParameter("month");
326
+
327
+ if(month != null && month.length() <= 2 && Integer.parseInt(month) <= 12 && Integer.parseInt(month) >= 1) {
328
+
329
+ count++;
330
+
331
+ }else {
332
+
333
+ e = "生まれ月は1~12で入力してください";
334
+
335
+ db.setError(e);
336
+
337
+ request.getRequestDispatcher("/jsp/Change_Update.jsp").forward(request, response);
338
+
339
+ }
340
+
341
+ //日
342
+
343
+ String date = request.getParameter("date");
344
+
345
+ if(date != null && date.length() <= 2 && Integer.parseInt(date) <=31 && Integer.parseInt(date) >=1) {
346
+
347
+ count++;
348
+
349
+ }else {
350
+
351
+ e = "生まれ日は1~31で入力してください";
352
+
353
+ db.setError(e);
354
+
355
+ request.getRequestDispatcher("/jsp/Change_Update.jsp").forward(request, response);
356
+
357
+ }
358
+
359
+ //血液
360
+
361
+ String blood = request.getParameter("blood");
362
+
363
+ if(blood != null) {
364
+
365
+ count++;
366
+
367
+ }else {
368
+
369
+ e = "血液型を選択してください";
370
+
371
+ db.setError(e);
372
+
373
+ request.getRequestDispatcher("/jsp/Change_Update.jsp").forward(request, response);
374
+
375
+ }
376
+
377
+ //意見
378
+
379
+ String opinion = request.getParameter("opinion");
380
+
381
+ if(opinion != null && opinion.length() <= 200) {
382
+
383
+ count++;
384
+
385
+ }else {
386
+
387
+ e = "ご意見は200文字まで入力できます";
388
+
389
+ db.setError(e);
390
+
391
+ request.getRequestDispatcher("/jsp/Change_Update.jsp").forward(request, response);
392
+
393
+ }
394
+
395
+ //総合
396
+
397
+ if(count == 10) {
398
+
399
+ //SQL文の準備(すべてのカラムにアンケート内容を入れるので、prepareを使って後からバインドさせる)
400
+
401
+ String sql ="UPDATE Information SET POSTNUM = ? , ADDRESS = ? , BIRTHYEAR = ? , BIRTHMONTH = ? , BIRTHDAY = ? , PHONE1 = ? , PHONE2 = ? , PHONE3 = ? , BLOOD = ? , BREAKFAST = ? , OPINION = ? WHERE NO = ?";
402
+
403
+ //接続準備
404
+
405
+ Connection conn = null;
406
+
407
+ PreparedStatement smt = null;
408
+
409
+ //更新のコード
410
+
411
+ if(request.getParameter("move") == "update") {
412
+
413
+ //接続開始
414
+
415
+ try {
416
+
417
+ conn = DBManager.getConnection();
418
+
419
+ //バインド用のSQLを準備
420
+
421
+ smt = conn.prepareStatement(sql);
422
+
423
+
424
+
425
+ //バインド開始
426
+
427
+ smt.setString(1, (request.getParameter("post_num1") + request.getParameter("post_num2")));
428
+
429
+ smt.setString(2, request.getParameter("address"));
430
+
431
+ smt.setString(3, request.getParameter("year"));
432
+
433
+ smt.setString(4, request.getParameter("month"));
434
+
435
+ smt.setString(5, request.getParameter("date"));
436
+
437
+ smt.setString(6, request.getParameter("phone_num1"));
438
+
439
+ smt.setString(7, request.getParameter("phone_num2"));
440
+
441
+ smt.setString(8, request.getParameter("phone_num3"));
442
+
443
+ smt.setString(9, request.getParameter("blood"));
444
+
445
+ smt.setString(10, request.getParameter("food"));
446
+
447
+ smt.setString(11, request.getParameter("opinion"));
448
+
449
+ smt.setString(12, request.getParameter("hiddenNO"));
450
+
451
+ //実行
452
+
453
+ smt.executeQuery();
454
+
455
+
456
+
457
+ }catch(SQLException er) {
458
+
459
+ throw new ServletException(er);
460
+
461
+ }finally {
462
+
463
+ if(smt != null) {
464
+
465
+ try {smt.close();}catch(SQLException ignore) {}
466
+
467
+ }
468
+
469
+ if(conn != null) {
470
+
471
+ try {conn.close();}catch(SQLException ignore) {}
472
+
473
+ }
474
+
475
+ }
476
+
477
+ //登録をする場合のコード
478
+
479
+ }else if(request.getParameter("move") == "regist") {
480
+
481
+ //insertさせるNoは、今テーブルに入っている数+1なので、countを求めるSQL
482
+
483
+ String sql2 = "SELECT count(No) FROM Information";
484
+
485
+ Statement smt2 = null;
486
+
487
+
488
+
489
+ //接続開始
490
+
491
+ try {
492
+
493
+ conn = DBManager.getConnection();
494
+
495
+ //バインド用のSQLを準備
496
+
497
+ smt = conn.prepareStatement(sql);
498
+
499
+ //count用のSQL準備&実行
500
+
501
+ smt2 = conn.createStatement();
502
+
503
+ count = smt2.executeUpdate(sql2);
504
+
505
+
506
+
507
+ //バインド開始
508
+
509
+ smt.setInt(1, count+1);//NOやからここより前にcount(No)の値をselectしておかないといけない。それプラス1
510
+
511
+ smt.setString(2, (request.getParameter("post_num1") + request.getParameter("post_num2")));
512
+
513
+ smt.setString(3, request.getParameter("address"));
514
+
515
+ smt.setString(4, request.getParameter("year"));
516
+
517
+ smt.setString(5, request.getParameter("month"));
518
+
519
+ smt.setString(6, request.getParameter("date"));
520
+
521
+ smt.setString(7, request.getParameter("phone_num1"));
522
+
523
+ smt.setString(8, request.getParameter("phone_num2"));
524
+
525
+ smt.setString(9, request.getParameter("phone_num3"));
526
+
527
+ smt.setString(10, request.getParameter("blood"));
528
+
529
+ smt.setString(11, request.getParameter("food"));
530
+
531
+ smt.setString(12, request.getParameter("opinion"));
532
+
533
+ //実行
534
+
535
+ smt.executeQuery();
536
+
537
+
538
+
539
+ }catch(SQLException er) {
540
+
541
+ throw new ServletException(er);
542
+
543
+ }finally {
544
+
545
+ if(smt != null) {
546
+
547
+ try {smt.close();}catch(SQLException ignore) {}
548
+
549
+ }
550
+
551
+ if(conn != null) {
552
+
553
+ try {conn.close();}catch(SQLException ignore) {}
554
+
555
+ }
556
+
557
+ }
558
+
559
+ }
560
+
561
+
562
+
563
+
564
+
565
+ request.getRequestDispatcher("/jsp/result.jsp").forward(request, response);
566
+
567
+ }
568
+
569
+ doGet(request, response);
570
+
571
+ }
572
+
573
+
574
+
575
+ }
576
+
577
+
578
+
579
+ ```