質問編集履歴

2

CheckからhiddenでFinishにformの内容を送れるように。Mapperにメソッドを作りました。

2019/10/27 07:32

投稿

mimi_129
mimi_129

スコア63

test CHANGED
File without changes
test CHANGED
@@ -108,6 +108,12 @@
108
108
 
109
109
  <%
110
110
 
111
+
112
+
113
+ request.setCharacterEncoding("UTF-8");
114
+
115
+
116
+
111
117
  PostMaster pm = new PostMaster();
112
118
 
113
119
 
@@ -116,6 +122,8 @@
116
122
 
117
123
  pm.setHonbun(request.getParameter("honbun"));
118
124
 
125
+
126
+
119
127
  %>
120
128
 
121
129
  <!DOCTYPE html>
@@ -128,7 +136,7 @@
128
136
 
129
137
  <link rel="stylesheet" href="../../css/stylesheet.css" type="text/css">
130
138
 
131
- <title>イト</title>
139
+ <title>投稿確認|ペット移動トリミングサイト</title>
132
140
 
133
141
  </head>
134
142
 
@@ -152,9 +160,141 @@
152
160
 
153
161
  <div id="content">
154
162
 
163
+ <ul>
164
+
165
+ <li>【タイトル】</li>
166
+
167
+ <li>
168
+
169
+ <%
170
+
171
+ out.println(pm.getTitle());
172
+
173
+ %>
174
+
175
+ </li>
176
+
177
+ <li>【本文】</li>
178
+
179
+ <li>
180
+
181
+ <%
182
+
183
+ out.println(pm.getHonbun());
184
+
185
+ %>
186
+
187
+ </li>
188
+
189
+ </ul>
190
+
155
191
  <form method="POST" action="post_finish.jsp">
156
192
 
193
+ <input type="hidden" name="title" value="<% pm.getTitle(); %>">
194
+
195
+ <input type="hidden" name="honbun" value="<% pm.getHonbun(); %>">
196
+
197
+ <input type="submit" value="送信">
198
+
199
+ </form>
200
+
201
+ </div>
202
+
203
+ <div id="side"></div>
204
+
205
+ <div id="footer">
206
+
207
+ </div>
208
+
209
+ </div>
210
+
211
+ </body>
212
+
213
+ </html>
214
+
215
+ ```
216
+
217
+
218
+
219
+ POST_FINISH
220
+
221
+ ```Java
222
+
223
+ <%@ page language="java" contentType="text/html; charset=UTF-8"
224
+
225
+ pageEncoding="UTF-8"%>
226
+
227
+ <%@ page import="model.PostMaster" %>
228
+
229
+ <%@ page import="model.PostMasterMapper" %>
230
+
231
+
232
+
233
+ <%
234
+
235
+
236
+
237
+ request.setCharacterEncoding("UTF-8");
238
+
239
+
240
+
241
+ PostMaster pm = new PostMaster();
242
+
243
+
244
+
245
+ pm.setTitle(request.getParameter("title"));
246
+
247
+ pm.setHonbun(request.getParameter("honbun"));
248
+
249
+
250
+
251
+ PostMasterMapper pmm = new PostMasterMapper();
252
+
253
+
254
+
255
+ pmm.addSQL();
256
+
257
+
258
+
259
+ %>
260
+
261
+ <!DOCTYPE html>
262
+
263
+ <html lang="en">
264
+
265
+ <head>
266
+
267
+ <meta charset="UTF-8">
268
+
269
+ <link rel="stylesheet" href="../../css/stylesheet.css" type="text/css">
270
+
271
+ <title>投稿完了|ペット移動トリミングサイト</title>
272
+
273
+ </head>
274
+
275
+ <body>
276
+
277
+ <div id="main">
278
+
279
+ <div id="header">
280
+
281
+ <p><a href="../index.jsp">ロゴ</a>
282
+
157
- <ul>
283
+ <ul>
284
+
285
+ <li>投稿画面へ</li>
286
+
287
+ <li>業者検索</li>
288
+
289
+ </ul>
290
+
291
+ </div>
292
+
293
+ <div id="content">
294
+
295
+ <p>投稿が完了しました</p>
296
+
297
+ <ul>
158
298
 
159
299
  <li>【タイトル】</li>
160
300
 
@@ -182,11 +322,9 @@
182
322
 
183
323
  </ul>
184
324
 
185
- <input type="submit" value="送信">
325
+ <p><a href="../index.jsp">TOPに戻る</a></p>
186
-
187
- </form>
326
+
188
-
189
- </div>
327
+ </div>
190
328
 
191
329
  <div id="side"></div>
192
330
 
@@ -204,87 +342,69 @@
204
342
 
205
343
 
206
344
 
207
- POST_FINISH
345
+ POST_MASTER
208
346
 
209
347
  ```Java
210
348
 
211
- <%@ page language="java" contentType="text/html; charset=UTF-8"
212
-
213
- pageEncoding="UTF-8"%>
214
-
215
- <%@ page import="model.PostMaster" %>
216
-
217
- <%@ page import="model.PostMasterMapper" %>
218
-
219
-
220
-
221
- <%
222
-
223
-
224
-
225
- PostMasterMapper pmm = new PostMasterMapper();
226
-
227
-
228
-
229
- %>
230
-
231
- <!DOCTYPE html>
232
-
233
- <html lang="en">
234
-
235
- <head>
236
-
237
- <meta charset="UTF-8">
238
-
239
- <link rel="stylesheet" href="../../css/stylesheet.css" type="text/css">
240
-
241
- <title>タイトル</title>
242
-
243
- </head>
244
-
245
- <body>
246
-
247
- <div id="main">
248
-
249
- <div id="header">
250
-
251
- <p><a href="../index.jsp">ロゴ</a>
252
-
253
- <ul>
254
-
255
- <li>投稿</li>
256
-
257
- <li>検索</li>
258
-
259
- </ul>
260
-
261
- </div>
262
-
263
- <div id="content">
264
-
265
- <p>投稿が完了しました</p>
266
-
267
- <p><a href="../index.jsp">TOPに戻る</a></p>
268
-
269
- </div>
270
-
271
- <div id="side"></div>
272
-
273
- <div id="footer">
274
-
275
- </div>
276
-
277
- </div>
278
-
279
- </body>
280
-
281
- </html>
282
-
283
- ```
284
-
285
-
286
-
287
- POST_MASTER
349
+ package model;
350
+
351
+
352
+
353
+ public class PostMaster {
354
+
355
+
356
+
357
+ // フィールド
358
+
359
+ private String title;
360
+
361
+ private String honbun;
362
+
363
+
364
+
365
+ // アクセサー
366
+
367
+ public String getTitle() {
368
+
369
+ return title;
370
+
371
+ }
372
+
373
+
374
+
375
+ public void setTitle(String title) {
376
+
377
+ this.title = title;
378
+
379
+ }
380
+
381
+
382
+
383
+ public String getHonbun() {
384
+
385
+ return honbun;
386
+
387
+ }
388
+
389
+
390
+
391
+ public void setHonbun(String honbun) {
392
+
393
+ this.honbun = honbun;
394
+
395
+ }
396
+
397
+
398
+
399
+ }
400
+
401
+
402
+
403
+ ```
404
+
405
+
406
+
407
+ POST_MASTERMAPPER
288
408
 
289
409
  ```Java
290
410
 
@@ -292,75 +412,13 @@
292
412
 
293
413
 
294
414
 
295
- public class PostMaster {
296
-
297
-
298
-
299
- // フィールド
300
-
301
- private String title;
302
-
303
- private String honbun;
304
-
305
-
306
-
307
- // アクセサー
308
-
309
- public String getTitle() {
310
-
311
- return title;
312
-
313
- }
314
-
315
-
316
-
317
- public void setTitle(String title) {
318
-
319
- this.title = title;
320
-
321
- }
322
-
323
-
324
-
325
- public String getHonbun() {
326
-
327
- return honbun;
328
-
329
- }
330
-
331
-
332
-
333
- public void setHonbun(String honbun) {
334
-
335
- this.honbun = honbun;
336
-
337
- }
338
-
339
-
340
-
341
- }
342
-
343
-
344
-
345
- ```
346
-
347
-
348
-
349
- POST_MASTERMAPPER
350
-
351
- ```Java
352
-
353
- package model;
354
-
355
-
356
-
357
415
  import java.sql.Connection;
358
416
 
359
417
  import java.sql.DriverManager;
360
418
 
361
419
  import java.sql.PreparedStatement;
362
420
 
363
- import java.sql.Statement;
421
+ import java.sql.SQLException;
364
422
 
365
423
 
366
424
 
@@ -368,57 +426,107 @@
368
426
 
369
427
 
370
428
 
371
- public static void main(String[] args) {
429
+ public int addSQL() throws SQLException {
372
430
 
373
431
 
374
432
 
375
433
  try {
376
434
 
435
+ //変数定義
436
+
437
+ Connection conn = null;
438
+
439
+ PreparedStatement ps = null;
440
+
441
+
442
+
377
- new PostMasterMapper().execute();
443
+ PostMaster pm = new PostMaster();
444
+
445
+
446
+
378
-
447
+ final String URL = "jdbc:mysql://localhost:8880/petidotriming";
448
+
449
+ final String USERNAME = "root";
450
+
451
+ final String PASSWORD = "kimitunagi5emu";
452
+
453
+
454
+
455
+ String sql = "INSERT INTO postmaster values(?, ?)";
456
+
457
+
458
+
459
+ //JDBCドライバをロードする
460
+
461
+ Class.forName("oracle.jdbc.driver.OracleDriver");
462
+
463
+
464
+
465
+ //DBへのコネクションを作成する
466
+
467
+ conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
468
+
469
+ conn.setAutoCommit(false); //オートコミットはオフ
470
+
471
+
472
+
473
+ //実行するSQL文とパラメータを指定する
474
+
475
+ ps = conn.prepareStatement(sql);
476
+
477
+ ps.setString(1, "タイトルです");
478
+
479
+ ps.setString(2, "本文です");
480
+
481
+
482
+
483
+ //INSERT文を実行する
484
+
485
+ int i = ps.executeUpdate();
486
+
487
+
488
+
489
+ //処理件数を表示する
490
+
491
+ System.out.println("結果:" + i);
492
+
493
+
494
+
495
+ //コミット
496
+
497
+ conn.commit();
498
+
499
+
500
+
501
+ //クローズ処理
502
+
503
+ if (ps != null) ps.close();
504
+
505
+ if (conn != null) conn.close();
506
+
507
+
508
+
509
+ return i;
510
+
511
+
512
+
379
- }catch(Exception e) {
513
+ } catch (Exception ex) {
514
+
380
-
515
+ //例外発生時の処理
516
+
381
- e.printStackTrace();
517
+ ex.printStackTrace(); //エラー内容をコンソールに出力する
518
+
519
+ return 2;
520
+
521
+ } finally {
522
+
523
+
382
524
 
383
525
  }
384
526
 
385
- }
527
+
386
-
387
-
388
-
389
- public void execute() throws Exception {
528
+
390
-
391
-
392
-
393
- PostMaster pm = new PostMaster();
529
+
394
-
395
-
396
-
397
- final String URL = "jdbc:mysql://localhost:8880/XXX";
398
-
399
- final String USERNAME = "XXX";
400
-
401
- final String PASSWORD = "XXX";
402
-
403
-
404
-
405
- Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
406
-
407
- Statement statement = conn.createStatement();
408
-
409
-
410
-
411
- String sql = "INSERT INTO postmaster (title, honbun) VALUES (?, ?)";
412
-
413
- PreparedStatement pstmt = conn.prepareStatement(sql);
414
-
415
- pstmt.setString(1, pm.getTitle());
416
-
417
- pstmt.setString(2, pm.getHonbun());
418
-
419
-
420
-
421
- int num = pstmt.executeUpdate();
422
530
 
423
531
 
424
532
 
@@ -426,4 +534,6 @@
426
534
 
427
535
 
428
536
 
537
+
538
+
429
- ```
539
+ ```

1

修正

2019/10/27 07:32

投稿

mimi_129
mimi_129

スコア63

test CHANGED
File without changes
test CHANGED
@@ -128,7 +128,7 @@
128
128
 
129
129
  <link rel="stylesheet" href="../../css/stylesheet.css" type="text/css">
130
130
 
131
- <title>投稿確認|ペット移動トリミングサイト</title>
131
+ <title>イト</title>
132
132
 
133
133
  </head>
134
134