質問編集履歴

2

内容修正

2020/09/08 03:37

投稿

ysda
ysda

スコア65

test CHANGED
File without changes
test CHANGED
@@ -416,6 +416,8 @@
416
416
 
417
417
  ```
418
418
 
419
+ //省略
420
+
419
421
  public class TestResultsRegistEditServlet extends HttpServlet{
420
422
 
421
423
 
@@ -446,9 +448,7 @@
446
448
 
447
449
  }
448
450
 
449
- request.setAttribute("status", status);
451
+
450
-
451
- request.getRequestDispatcher("test-results-regist.jsp").forward(request, response);
452
452
 
453
453
  } catch(Exception e) {
454
454
 
@@ -460,6 +460,10 @@
460
460
 
461
461
  conn.close();
462
462
 
463
+             request.setAttribute("status", status);
464
+
465
+    request.getRequestDispatcher("test-results-regist.jsp").forward(request, response);
466
+
463
467
  } catch (SQLException e) {
464
468
 
465
469
  System.out.println("MySQLのクローズに失敗しました。");

1

ソースの修正・追加

2020/09/08 03:37

投稿

ysda
ysda

スコア65

test CHANGED
File without changes
test CHANGED
@@ -24,315 +24,201 @@
24
24
 
25
25
 
26
26
 
27
- ### 該当のソースコード
27
+ ### 該当のソースコード(文字制限の関係上、削除機能部分のみの記載です)
28
-
29
- (サーブレットは文字数制限のため省略)
30
28
 
31
29
  test-results-regist.jsp
32
30
 
33
31
  ```ここに言語名を入力
34
32
 
35
- <!-- 省略 -->
33
+ <!--省略 -->
36
-
37
-
38
-
39
- <div class="container">
34
+
40
-
41
- <form action="testResultsEdit" method="post">
35
+ <form action="testResultsRegistEdit" method="post">
36
+
42
-
37
+ <table class="input-form-table">
38
+
39
+ <tr>
40
+
41
+ <td>試験日</td>
42
+
43
+ <td><select name="test_day">
44
+
45
+ <option value="">すべての試験日</option>
46
+
47
+ <!-- 試験日のプルダウン記述 -->
48
+
49
+ </select></td>
50
+
51
+ </tr>
52
+
53
+ <tr>
54
+
55
+ <td>学年</td>
56
+
57
+ <td><input type="text" id="year" name="school_year"
58
+
59
+ maxlength='10' placeholder="学年(半角数字)"></td>
60
+
61
+ </tr>
62
+
63
+ <tr>
64
+
65
+ <td>クラス</td>
66
+
67
+ <td><input type="text" id="class" name="school_class"
68
+
69
+ maxlength='10' placeholder="クラス(半角数字)"></td>
70
+
71
+ </tr>
72
+
73
+ </table>
74
+
75
+
76
+
77
+ <div class="buttons">
78
+
79
+ <div class="submit-button text-center test-results-button">
80
+
43
- <input type="hidden" name="mode" value="preSearch">
81
+ <button type="submit" name="mode" value="search">検索</button>
82
+
44
-
83
+ </div>
84
+
85
+
86
+
87
+ <div class="submit-button text-center test-results-button">
88
+
45
- <input type="submit" class="btn btn-primary btn-lg" value="試験結果">
89
+ <button type="button" onclick="clearField()">クリア</button>
90
+
91
+ </div>
92
+
93
+ </div>
94
+
95
+
46
96
 
47
97
  </form>
48
98
 
49
-
50
-
51
- <!--省略 -->
52
-
53
-
54
-
55
- <ul class="nav nav-tabs">
56
-
57
- <li class="nav-item"><a href="#search" class="nav-link active"
58
-
59
- data-toggle="tab">試験結果検索</a></li>
60
-
61
- <li class="nav-item"><a href="#regist" class="nav-link"
62
-
63
- data-toggle="tab">試験結果登録</a></li>
64
-
65
- </ul>
66
-
67
-
68
-
69
- <div class="tab-content">
70
-
71
- <div id="search" class="tab-pane active">
72
-
73
- <h2>試験結果検索</h2>
74
-
75
- <div class="input-form">
76
-
77
- <form action="testResultsRegistEdit" method="post">
78
-
79
- <table class="input-form-table">
80
-
81
- <tr>
82
-
83
- <td>試験日</td>
84
-
85
- <td>
86
-
87
- <select name="test_day">
88
-
89
- <option value="">すべての試験日</option>
90
-
91
- <%
92
-
93
- if(preTestDayResult != null) {
94
-
95
- %>
96
-
97
- <%
98
-
99
- while(preTestDayResult.next()) {
100
-
101
- %>
102
-
103
- <option value="<%=preTestDayResult.getString(1) %>"><%=preTestDayResult.getString(1) %></option>
104
-
105
- <%
106
-
107
- }
108
-
109
- %>
110
-
111
- <%
112
-
113
- }
114
-
115
- %>
116
-
117
- </select>
118
-
119
- </td>
120
-
121
- </tr>
122
-
123
- <tr>
124
-
125
- <td>学年</td>
126
-
127
- <td>
128
-
129
- <%if(school_year_searched != null) {%>
130
-
131
- <input type="text" id="year" name="school_year" maxlength='10' value="<%=school_year_searched %>"
132
-
133
- placeholder="学年(半角数字)">
134
-
135
- <%} else {%>
136
-
137
- <input type="text" id="year" name="school_year" maxlength='10'
138
-
139
- placeholder="学年(半角数字)">
140
-
141
- <%} %>
142
-
143
- <%if(input_year_err != null) {%>
144
-
145
- <p><font color="red"><%=input_year_err %></font></p>
146
-
147
- <%} %>
148
-
149
- </td>
150
-
151
- </tr>
152
-
153
- <tr>
154
-
155
- <td>クラス</td>
156
-
157
- <td>
158
-
159
- <%if(school_class_searched != null) {%>
160
-
161
- <input type="text" id="class" name="school_class" maxlength='10' value="<%=school_class_searched %>"
162
-
163
- placeholder="クラス(半角数字)">
164
-
165
- <%} else {%>
166
-
167
- <input type="text" id="class" name="school_class" maxlength='10'
168
-
169
- placeholder="クラス(半角数字)">
170
-
171
- <%} %>
172
-
173
- <%if(input_class_err != null) {%>
174
-
175
- <p><font color="red"><%=input_class_err %></font></p>
176
-
177
- <%} %>
178
-
179
- </td>
180
-
181
- </tr>
182
-
183
- </table>
184
-
185
-
186
-
187
- <div class="buttons">
188
-
189
- <div class="submit-button text-center test-results-button">
190
-
191
- <button type="submit" name="mode" value="search">検索</button>
192
-
193
- </div>
194
-
195
-
196
-
197
- <div class="submit-button text-center test-results-button">
198
-
199
- <button type="button" name="mode" onclick="clearField()">クリア</button>
200
-
201
- </div>
202
-
203
- </div>
204
-
205
-
206
-
207
- </form>
208
-
209
- </div>
210
-
211
-
212
-
213
- <%if(testResults != null) {%>
214
-
215
- <%for(TestResult data: testResults) {%>
216
-
217
-
218
-
219
- <table class="table search-result-table">
220
-
221
- <thead>
222
-
223
- <tr>
224
-
225
- <th>学年</th>
226
-
227
- <th>クラス</th>
228
-
229
- <th>生徒ID</th>
230
-
231
- <th>名前</th>
232
-
233
- </tr>
234
-
235
- </thead>
236
-
237
-
238
-
239
- <tbody>
240
-
241
- <tr>
242
-
243
- <td><%=data.getSchool_year() %></td>
244
-
245
- <td><%=data.getSchool_class() %></td>
246
-
247
- <td>
248
-
249
- <%=data.getStudent_id() %>
250
-
251
- </td>
252
-
253
- <td><%=data.getStudent_name() %></td>
254
-
255
- </tr>
256
-
257
- </tbody>
258
-
259
- <thead>
260
-
261
- <tr>
262
-
263
- <th>試験日</th>
264
-
265
- <th>科目</th>
266
-
267
- <th>得点</th>
268
-
269
- <th>更新</th>
270
-
271
- <th>削除</th>
272
-
273
- </tr>
274
-
275
- </thead>
276
-
277
- <%for (TestResult data2: testResults2) {%>
278
-
279
- <%if (data2.getStudent_id().equals(data.getStudent_id())) {%>
280
-
281
- <form action="testResultsRegistEdit" method="post">
282
-
283
- <input type="hidden" name="student_id" value="<%=data.getStudent_id() %>"><!--生徒IDをsubmit -->
284
-
285
- <tr>
286
-
287
- <td>
288
-
289
- <input type="date" name="test_day" value="<%=data2.getTest_day() %>" max="2999-12-31"><!--試験日をsubmit -->
290
-
291
- </td>
292
-
293
- <td>
294
-
295
- <%=data2.getSubject_name() %>
296
-
297
- <input type="hidden" name="subject_id" value="<%=data2.getSubject_id() %>"><!--教科IDをsubmit -->
298
-
299
- </td>
300
-
301
- <td>
302
-
303
- <input type="text" name="score" value="<%=data2.getScore() %>"><!--得点をsubmit -->
304
-
305
- </td>
306
-
307
- <td>
308
-
309
- <button type="submit" name="mode" value="update">更新</button>
310
-
311
- </td>
312
-
313
- <td>
314
-
315
- <button type="submit" name="mode" value="delete">削除</button>
316
-
317
- </td>
318
-
319
- </tr>
320
-
321
- </form>
322
-
323
- <%} %>
324
-
325
- <%} %>
326
-
327
- </table>
328
-
329
-
330
-
331
- <%} %>
332
-
333
- <%} %>
334
-
335
- </div>
99
+ </div>
100
+
101
+
102
+
103
+ <%if(testResults != null) {%>
104
+
105
+ <%for(TestResult data: testResults) {%>
106
+
107
+
108
+
109
+ <table class="table search-result-table">
110
+
111
+ <thead>
112
+
113
+ <tr>
114
+
115
+ <th>学年</th>
116
+
117
+ <th>クラス</th>
118
+
119
+ <th>生徒ID</th>
120
+
121
+ <th>名前</th>
122
+
123
+ </tr>
124
+
125
+ </thead>
126
+
127
+
128
+
129
+ <tbody>
130
+
131
+ <tr>
132
+
133
+ <td><%=data.getSchool_year() %></td>
134
+
135
+ <td><%=data.getSchool_class() %></td>
136
+
137
+ <td><%=data.getStudent_id() %></td>
138
+
139
+ <td><%=data.getStudent_name() %></td>
140
+
141
+ </tr>
142
+
143
+ </tbody>
144
+
145
+ <thead>
146
+
147
+ <tr>
148
+
149
+ <th>試験日</th>
150
+
151
+ <th>科目</th>
152
+
153
+ <th>得点</th>
154
+
155
+ <th>更新</th>
156
+
157
+ <th>削除</th>
158
+
159
+ </tr>
160
+
161
+ </thead>
162
+
163
+ <%for (TestResult data2: testResults2) {%>
164
+
165
+ <%if (data2.getStudent_id().equals(data.getStudent_id())) {%>
166
+
167
+ <form action="testResultsRegistEdit" method="post">
168
+
169
+ <input type="hidden" name="student_id"
170
+
171
+ value="<%=data.getStudent_id() %>">
172
+
173
+ <!--生徒IDをsubmit -->
174
+
175
+ <tr>
176
+
177
+ <td><input type="date" name="test_day"
178
+
179
+ value="<%=data2.getTest_day() %>" max="2999-12-31">
180
+
181
+ <!--試験日をsubmit --></td>
182
+
183
+ <td><%=data2.getSubject_name() %> <input type="hidden"
184
+
185
+ name="subject_id" value="<%=data2.getSubject_id() %>">
186
+
187
+ <!--教科IDをsubmit --></td>
188
+
189
+ <td><input type="text" name="score"
190
+
191
+ value="<%=data2.getScore() %>">
192
+
193
+ <!--得点をsubmit --></td>
194
+
195
+ <td>
196
+
197
+ <button type="submit" name="mode" value="update">更新</button>
198
+
199
+ </td>
200
+
201
+ <td>
202
+
203
+ <button type="submit" name="mode" value="delete">削除</button>
204
+
205
+ </td>
206
+
207
+ </tr>
208
+
209
+ </form>
210
+
211
+ <%} %>
212
+
213
+ <%} %>
214
+
215
+ </table>
216
+
217
+
218
+
219
+ <%} %>
220
+
221
+ <%} %>
336
222
 
337
223
  <!--以下省略 -->
338
224
 
@@ -440,13 +326,13 @@
440
326
 
441
327
 
442
328
 
443
- public static boolean updateData() {
329
+ public static boolean deleteData() {
444
330
 
445
331
  try {
446
332
 
447
- System.out.println("更新処理開始");
333
+ System.out.println("削除処理開始");
448
-
334
+
449
- String sql = "UPDATE test_results SET score = ?, test_day = ? WHERE student_id = ? and subject_id = ?";
335
+ String sql = "DELETE FROM test_results WHERE student_id = ? and subject_id = ? and test_day = ?";
450
336
 
451
337
 
452
338
 
@@ -460,13 +346,21 @@
460
346
 
461
347
 
462
348
 
349
+ //以下の3つはすべてnullで出力される
350
+
463
- ps.setInt(1, Integer.parseInt(score));
351
+ System.out.println(student_id);
352
+
464
-
353
+ System.out.println(subject_id);
354
+
465
- ps.setDate(2, Date.valueOf(test_day));
355
+ System.out.println(test_day);
466
-
356
+
357
+
358
+
467
- ps.setInt(3, Integer.parseInt(student_id));
359
+ ps.setInt(1, Integer.parseInt(student_id));
468
-
360
+
469
- ps.setInt(4, Integer.parseInt(subject_id));
361
+ ps.setInt(2, Integer.parseInt(subject_id));
362
+
363
+ ps.setDate(3, Date.valueOf(test_day));
470
364
 
471
365
 
472
366
 
@@ -512,65 +406,55 @@
512
406
 
513
407
 
514
408
 
515
- public static boolean deleteData() {
516
-
517
- try {
518
-
519
- System.out.println("削除処理開始");
520
-
521
- String sql = "DELETE FROM test_results WHERE student_id = ? and subject_id = ? and test_day = ?";
522
-
523
-
524
-
525
- Class.forName("com.mysql.jdbc.Driver");
526
-
527
- conn = DriverManager.getConnection(url, user, password);
528
-
529
-
530
-
531
- ps = conn.prepareStatement(sql);
532
-
533
-
534
-
535
- //以下の3つはすべてnullで出力される
536
-
537
- System.out.println(student_id);
538
-
539
- System.out.println(subject_id);
540
-
541
- System.out.println(test_day);
542
-
543
-
544
-
545
- ps.setInt(1, Integer.parseInt(student_id));
546
-
547
- ps.setInt(2, Integer.parseInt(subject_id));
548
-
549
- ps.setDate(3, Date.valueOf(test_day));
550
-
551
-
552
-
553
- System.out.println(ps);
554
-
555
-
556
-
557
- ps.executeUpdate();
558
-
559
-
560
-
561
- return true;
562
-
563
-
564
-
565
- } catch(Exception e) {
566
-
567
- e.printStackTrace();
568
-
569
- return false;
570
-
571
- } finally {
572
-
573
- if(conn != null) {
409
+ }
410
+
411
+ ```
412
+
413
+ TestResultsRegistEditServlet.java
414
+
415
+ (かなり省略しました)
416
+
417
+ ```
418
+
419
+ public class TestResultsRegistEditServlet extends HttpServlet{
420
+
421
+
422
+
423
+ Connection conn = null;
424
+
425
+
426
+
427
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
428
+
429
+ request.setCharacterEncoding("UTF-8");
430
+
431
+ String mode = request.getParameter("mode");
432
+
433
+ String status = "Success!";
434
+
435
+
436
+
437
+ switch(mode) {
438
+
439
+ case "delete":
440
+
441
+ try {
442
+
443
+ if(TestResultsBeans.deleteData() == false) {
444
+
445
+ status = "Failed...";
446
+
447
+ }
448
+
449
+ request.setAttribute("status", status);
450
+
451
+ request.getRequestDispatcher("test-results-regist.jsp").forward(request, response);
452
+
453
+ } catch(Exception e) {
454
+
455
+ e.printStackTrace();
456
+
457
+ } finally {
574
458
 
575
459
  try {
576
460
 
@@ -578,137 +462,19 @@
578
462
 
579
463
  } catch (SQLException e) {
580
464
 
581
- e.printStackTrace();
582
-
583
465
  System.out.println("MySQLのクローズに失敗しました。");
584
466
 
585
467
  }
586
468
 
469
+ }
470
+
587
471
  }
588
472
 
473
+ break;
474
+
589
475
  }
590
476
 
591
- }
477
+
592
-
593
-
594
-
595
- public static boolean existTestData() {
596
-
597
- int bool = 0;
598
-
599
-
600
-
601
- try {
602
-
603
- Class.forName("com.mysql.jdbc.Driver");
604
-
605
- conn = DriverManager.getConnection(url, user, password);
606
-
607
-
608
-
609
- String sql = "select exists(select * from test_results where student_id = ? and subject_id = ?)";
610
-
611
-
612
-
613
- ps = conn.prepareStatement(sql);
614
-
615
-
616
-
617
- ps.setInt(1, Integer.parseInt(student_id));
618
-
619
- ps.setInt(2, Integer.parseInt(subject_id));
620
-
621
-
622
-
623
- System.out.println(ps);
624
-
625
-
626
-
627
- result = ps.executeQuery();
628
-
629
-
630
-
631
- while(result.next()) {
632
-
633
- bool = result.getInt(1);
634
-
635
- }
636
-
637
-
638
-
639
- if(bool == 1) {
640
-
641
- return true;
642
-
643
- } else {
644
-
645
- return false;
646
-
647
- }
648
-
649
-
650
-
651
- } catch(Exception e) {
652
-
653
- e.printStackTrace();
654
-
655
- return false;
656
-
657
- } finally {
658
-
659
- if(conn != null) {
660
-
661
- try {
662
-
663
- conn.close();
664
-
665
- } catch (SQLException e) {
666
-
667
- e.printStackTrace();
668
-
669
- System.out.println("MySQLのクローズに失敗しました。");
670
-
671
- }
672
-
673
- }
674
-
675
- }
676
-
677
- }
678
-
679
-
680
-
681
- public static boolean isNumber(String val) {
682
-
683
- String regex = "\A[-]?[0-9]+\z";
684
-
685
- Pattern p = Pattern.compile(regex);
686
-
687
- Matcher m1 = p.matcher(val);
688
-
689
- return m1.find();
690
-
691
- }
692
-
693
-
694
-
695
- public static boolean isUnder100(String str) {
696
-
697
- boolean scoreFlg;
698
-
699
- int score = Integer.parseInt(str);
700
-
701
- if(score > 100 || score < 0) {
702
-
703
- scoreFlg = false;
704
-
705
- } else {
706
-
707
- scoreFlg = true;
708
-
709
- }
710
-
711
- return scoreFlg;
712
478
 
713
479
  }
714
480