質問編集履歴

1

質問の内容およびServletのソースを修正いたしました。

2020/06/18 00:46

投稿

shogakusha
shogakusha

スコア12

test CHANGED
@@ -1 +1 @@
1
- 一覧画面から詳細画面遷移時、データが取得できず、nullとなってしまいます
1
+ DAOで設定した情報をServlet連携できず、値がnullとなってしまいます
test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  一覧画面の各検索結果に「詳細」リンクがあり、押下したら詳細画面へ遷移し、該当文書のタイトル、作者、内容が表示されるように実装したいです。
14
14
 
15
- その際に、IDをキーに、文書の内容等が表示されるようにしたいのすが、IDの情報を取得することができ、タイトル、作者、内容がすべてnullとなってしまいま
15
+ DAOIDの情報を取得するができ、タイトル、作者、内容の値を取得できているのです、Servletではタイトル、作者、内容の値がnullとなってしまい、理由が分かりせん
16
16
 
17
17
  初学者のため、質問に難有りかと思いますが、ご容赦ください。
18
18
 
@@ -206,412 +206,414 @@
206
206
 
207
207
  BookDto book = new BookDto(id, bookName, detail, author);
208
208
 
209
+ String idAsString = request.getParameter("id");
210
+
211
+ id = Integer.parseInt(idAsString);
212
+
213
+
214
+
215
+ DBManager db = new DBManager();
216
+
217
+
218
+
219
+ try {
220
+
221
+ BookDao bookDao = new BookDao(db);
222
+
223
+ ArrayList<BookDto> list = bookDao.find(id);
224
+
225
+
226
+
227
+ request.setCharacterEncoding("UTF8");
228
+
229
+ request.setAttribute("list", list);
230
+
231
+
232
+
233
+ if (list != null) {
234
+
235
+ bookName = book.getBooktitle();
236
+
237
+ detail = book.getBooktext();
238
+
239
+ author = book.getAuthor();
240
+
241
+ request.setAttribute("booktitle", bookName);
242
+
243
+ request.setAttribute("text", detail);
244
+
245
+ request.setAttribute("author", author);
246
+
247
+ RequestDispatcher dispatcher = request.getRequestDispatcher("/detail.jsp");
248
+
249
+ dispatcher.forward(request, response);
250
+
251
+ }
252
+
253
+ } catch (Exception e) {
254
+
255
+ e.printStackTrace();
256
+
257
+ }
258
+
259
+ }
260
+
261
+
262
+
263
+ }
264
+
265
+
266
+
267
+ ```
268
+
269
+ ```BookDao.java
270
+
271
+ package dao;
272
+
273
+
274
+
275
+ import java.sql.Connection;
276
+
277
+ import java.sql.PreparedStatement;
278
+
279
+ import java.sql.ResultSet;
280
+
281
+ import java.util.ArrayList;
282
+
283
+
284
+
285
+ import dba.DBManager;
286
+
287
+ import dto.BookDto;
288
+
289
+ import dto.BookSearchDto;
290
+
291
+
292
+
293
+ public class BookDao {
294
+
295
+ Connection conn = null;
296
+
297
+ PreparedStatement pStmt = null;
298
+
299
+ ResultSet rs = null;
300
+
301
+
302
+
303
+ private DBManager db;
304
+
305
+
306
+
307
+ public BookDao(DBManager db) {
308
+
309
+ this.db = db;
310
+
311
+ }
312
+
313
+
314
+
315
+ public ArrayList<BookSearchDto> findAll() throws Exception {
316
+
317
+ ArrayList<BookSearchDto> bookList = new ArrayList<BookSearchDto>();
318
+
319
+ String sql = "SELECT ID, BOOKTITLE, BOOKTEXT, AUTHOR, UPDATETIME, UPDATEUSER, INSERTTIME, INSERTUSER FROM T_BOOK";
320
+
321
+
322
+
323
+ Connection conn = db.getConnection();
324
+
325
+ PreparedStatement pStmt = conn.prepareStatement(sql);
326
+
327
+ ResultSet rs = pStmt.executeQuery();
328
+
329
+
330
+
331
+ while (rs.next()) {
332
+
333
+ int id = rs.getInt("ID");
334
+
335
+ String booktitle = rs.getString("BOOKTITLE");
336
+
337
+
338
+
339
+ String author = rs.getString("AUTHOR");
340
+
341
+
342
+
343
+ BookSearchDto bookSearch = new BookSearchDto(id, booktitle, author);
344
+
345
+ bookList.add(bookSearch);
346
+
347
+ }
348
+
349
+ return bookList;
350
+
351
+ }
352
+
353
+
354
+
355
+ public ArrayList<BookSearchDto> find(String bookName) throws Exception {
356
+
357
+
358
+
359
+ ArrayList<BookSearchDto> bookList = new ArrayList<BookSearchDto>();
360
+
361
+ String sql = "SELECT ID, BOOKTITLE, AUTHOR FROM T_BOOK WHERE BOOKTITLE LIKE ?";
362
+
363
+
364
+
365
+ Connection conn = db.getConnection();
366
+
367
+
368
+
369
+ PreparedStatement pStmt = conn.prepareStatement(sql);
370
+
371
+
372
+
373
+ pStmt.setString(1, bookName + "%");
374
+
375
+
376
+
377
+ ResultSet rs = pStmt.executeQuery();
378
+
379
+
380
+
381
+ while (rs.next()) {
382
+
383
+ int id = rs.getInt("ID");
384
+
385
+ bookName = rs.getString("BOOKTITLE");
386
+
387
+ String author = rs.getString("AUTHOR");
388
+
389
+ BookSearchDto bookSearch = new BookSearchDto(id, bookName, author);
390
+
391
+ bookList.add(bookSearch);
392
+
393
+
394
+
395
+ }
396
+
397
+ return bookList;
398
+
399
+ }
400
+
401
+
402
+
403
+ public ArrayList<BookDto> find(int id) throws Exception {
404
+
405
+ String booktitle = null;
406
+
407
+ String text = null;
408
+
409
+ String author = null;
410
+
411
+
412
+
413
+ BookDto book = new BookDto(id, booktitle, text, author);
414
+
415
+ ArrayList<BookDto> list = new ArrayList<BookDto>();
416
+
417
+ String sql = "SELECT BOOKTITLE, BOOKTEXT, AUTHOR FROM T_BOOK WHERE ID = ?";
418
+
419
+
420
+
421
+ Connection conn = db.getConnection();
422
+
423
+
424
+
425
+ PreparedStatement pStmt = conn.prepareStatement(sql);
426
+
209
427
  id = book.getId();
210
428
 
211
-
429
+ pStmt.setInt(1, id);
212
-
430
+
431
+
432
+
213
- DBManager db = new DBManager();
433
+ ResultSet rs = pStmt.executeQuery();
214
-
215
-
216
-
434
+
435
+
436
+
217
- try {
437
+ while (rs.next()) {
218
-
438
+
219
- BookDao bookDao = new BookDao(db);
439
+ id = rs.getInt("ID");
220
-
440
+
221
- ArrayList<BookDto> list = bookDao.find(id);
441
+ booktitle = rs.getString("BOOKTITLE");
442
+
222
-
443
+ text = rs.getString("BOOKTEXT");
223
-
224
-
444
+
225
- request.setCharacterEncoding("UTF8");
445
+ author = rs.getString("AUTHOR");
226
-
227
- request.setAttribute("list", list);
446
+
228
-
229
-
230
-
231
- if (list != null) {
447
+
232
-
233
- bookName = book.getBooktitle();
448
+
234
-
235
- detail = book.getBooktext();
236
-
237
- author = book.getAuthor();
238
-
239
- request.setAttribute("booktitle", bookName);
240
-
241
- request.setAttribute("text", detail);
242
-
243
- request.setAttribute("author", author);
449
+ book = new BookDto(id, booktitle, text, author);
244
-
245
- RequestDispatcher dispatcher = request.getRequestDispatcher("/detail.jsp");
450
+
246
-
247
- dispatcher.forward(request, response);
248
-
249
- }
250
-
251
- } catch (Exception e) {
252
-
253
- e.printStackTrace();
451
+ list.add(book);
254
452
 
255
453
  }
256
454
 
455
+ return list;
456
+
257
457
  }
258
458
 
259
459
 
260
460
 
461
+ public boolean regist(BookDto book) throws Exception {
462
+
463
+ boolean judge = false;
464
+
465
+ String sql = "INSERT INTO T_BOOK(BOOKTITLE, BOOKTEXT, AUTHOR) VALUES(?, ?, ?)";
466
+
467
+ String booktitle = book.getBooktitle();
468
+
469
+ String booktext = book.getBooktext();
470
+
471
+ String author = book.getAuthor();
472
+
473
+
474
+
475
+ Connection conn = db.getConnection();
476
+
477
+
478
+
479
+ PreparedStatement pStmt = conn.prepareStatement(sql);
480
+
481
+ pStmt.setString(1, booktitle);
482
+
483
+ pStmt.setString(2, booktext);
484
+
485
+ pStmt.setString(3, author);
486
+
487
+
488
+
489
+ int count = pStmt.executeUpdate();
490
+
491
+
492
+
493
+ if (count == 1) {
494
+
495
+ judge = true;
496
+
497
+ }
498
+
499
+ return judge;
500
+
501
+ }
502
+
503
+
504
+
505
+ public boolean update(BookDto book) throws Exception {
506
+
507
+ boolean judge = false;
508
+
509
+ String sql = "UODATE T_BOOK SET BOOKTITLE = ?, BOOKTEXT = ?, AUTHOR = ?";
510
+
511
+
512
+
513
+ String booktitle = book.getBooktitle();
514
+
515
+ String booktext = book.getBooktext();
516
+
517
+ String author = book.getAuthor();
518
+
519
+
520
+
521
+ Connection conn = db.getConnection();
522
+
523
+
524
+
525
+ PreparedStatement pStmt = conn.prepareStatement(sql);
526
+
527
+ pStmt.setString(1, booktitle);
528
+
529
+ pStmt.setString(2, booktext);
530
+
531
+ pStmt.setString(3, author);
532
+
533
+
534
+
535
+ int count = pStmt.executeUpdate();
536
+
537
+
538
+
539
+ if (count == 1) {
540
+
541
+ judge = true;
542
+
543
+ }
544
+
545
+ return judge;
546
+
547
+ }
548
+
261
549
  }
262
550
 
263
-
264
-
265
551
  ```
266
552
 
267
- ```BookDao.java
268
-
269
- package dao;
270
-
271
-
272
-
273
- import java.sql.Connection;
274
-
275
- import java.sql.PreparedStatement;
276
-
277
- import java.sql.ResultSet;
278
-
279
- import java.util.ArrayList;
280
-
281
-
282
-
283
- import dba.DBManager;
284
-
285
- import dto.BookDto;
286
-
287
- import dto.BookSearchDto;
288
-
289
-
290
-
291
- public class BookDao {
292
-
293
- Connection conn = null;
294
-
295
- PreparedStatement pStmt = null;
296
-
297
- ResultSet rs = null;
298
-
299
-
300
-
301
- private DBManager db;
302
-
303
-
304
-
305
- public BookDao(DBManager db) {
306
-
307
- this.db = db;
308
-
309
- }
310
-
311
-
312
-
313
- public ArrayList<BookSearchDto> findAll() throws Exception {
314
-
315
- ArrayList<BookSearchDto> bookList = new ArrayList<BookSearchDto>();
316
-
317
- String sql = "SELECT ID, BOOKTITLE, BOOKTEXT, AUTHOR, UPDATETIME, UPDATEUSER, INSERTTIME, INSERTUSER FROM T_BOOK";
318
-
319
-
320
-
321
- Connection conn = db.getConnection();
322
-
323
- PreparedStatement pStmt = conn.prepareStatement(sql);
324
-
325
- ResultSet rs = pStmt.executeQuery();
326
-
327
-
328
-
329
- while (rs.next()) {
330
-
331
- int id = rs.getInt("ID");
332
-
333
- String booktitle = rs.getString("BOOKTITLE");
334
-
335
-
336
-
337
- String author = rs.getString("AUTHOR");
338
-
339
-
340
-
341
- BookSearchDto bookSearch = new BookSearchDto(id, booktitle, author);
342
-
343
- bookList.add(bookSearch);
344
-
345
- }
346
-
347
- return bookList;
348
-
349
- }
350
-
351
-
352
-
353
- public ArrayList<BookSearchDto> find(String bookName) throws Exception {
354
-
355
-
356
-
357
- ArrayList<BookSearchDto> bookList = new ArrayList<BookSearchDto>();
358
-
359
- String sql = "SELECT ID, BOOKTITLE, AUTHOR FROM T_BOOK WHERE BOOKTITLE LIKE ?";
360
-
361
-
362
-
363
- Connection conn = db.getConnection();
364
-
365
-
366
-
367
- PreparedStatement pStmt = conn.prepareStatement(sql);
368
-
369
-
370
-
371
- pStmt.setString(1, bookName + "%");
372
-
373
-
374
-
375
- ResultSet rs = pStmt.executeQuery();
376
-
377
-
378
-
379
- while (rs.next()) {
380
-
381
- int id = rs.getInt("ID");
382
-
383
- bookName = rs.getString("BOOKTITLE");
384
-
385
- String author = rs.getString("AUTHOR");
386
-
387
- BookSearchDto bookSearch = new BookSearchDto(id, bookName, author);
388
-
389
- bookList.add(bookSearch);
390
-
391
-
392
-
393
- }
394
-
395
- return bookList;
396
-
397
- }
398
-
399
-
400
-
401
- public ArrayList<BookDto> find(int id) throws Exception {
402
-
403
- String booktitle = null;
404
-
405
- String text = null;
553
+ ```detail.jsp
554
+
555
+ <%@ page language="java" contentType="text/html; charset=UTF-8"
556
+
557
+ pageEncoding="UTF-8" import="java.io.*,java.util.*,java.text.*,dto.*"%>
558
+
559
+ <!DOCTYPE html">
560
+
561
+ <html>
562
+
563
+ <head>
564
+
565
+ <meta charset="UTF-8">
566
+
567
+ <title>詳細画面</title>
568
+
569
+ </head>
570
+
571
+ <body>
572
+
573
+ <%
574
+
575
+ // リストデータをリクエストから取得
576
+
577
+ int id = 0;
578
+
579
+ String bookName = null;
406
580
 
407
581
  String author = null;
408
582
 
409
-
410
-
411
- BookDto book = new BookDto(id, booktitle, text, author);
412
-
413
- ArrayList<BookDto> list = new ArrayList<BookDto>();
414
-
415
- String sql = "SELECT BOOKTITLE, BOOKTEXT, AUTHOR FROM T_BOOK WHERE ID = ?";
416
-
417
-
418
-
419
- Connection conn = db.getConnection();
420
-
421
-
422
-
423
- PreparedStatement pStmt = conn.prepareStatement(sql);
424
-
425
- id = book.getId();
426
-
427
- pStmt.setInt(1, id);
428
-
429
-
430
-
431
- ResultSet rs = pStmt.executeQuery();
432
-
433
-
434
-
435
- while (rs.next()) {
436
-
437
- id = rs.getInt("ID");
438
-
439
- booktitle = rs.getString("BOOKTITLE");
440
-
441
- text = rs.getString("BOOKTEXT");
442
-
443
- author = rs.getString("AUTHOR");
444
-
445
-
446
-
447
- book = new BookDto(id, booktitle, text, author);
448
-
449
- list.add(book);
450
-
451
- }
452
-
453
- return list;
454
-
455
- }
456
-
457
-
458
-
459
- public boolean regist(BookDto book) throws Exception {
460
-
461
- boolean judge = false;
462
-
463
- String sql = "INSERT INTO T_BOOK(BOOKTITLE, BOOKTEXT, AUTHOR) VALUES(?, ?, ?)";
464
-
465
- String booktitle = book.getBooktitle();
466
-
467
- String booktext = book.getBooktext();
468
-
469
- String author = book.getAuthor();
470
-
471
-
472
-
473
- Connection conn = db.getConnection();
474
-
475
-
476
-
477
- PreparedStatement pStmt = conn.prepareStatement(sql);
478
-
479
- pStmt.setString(1, booktitle);
480
-
481
- pStmt.setString(2, booktext);
482
-
483
- pStmt.setString(3, author);
484
-
485
-
486
-
487
- int count = pStmt.executeUpdate();
488
-
489
-
490
-
491
- if (count == 1) {
492
-
493
- judge = true;
494
-
495
- }
496
-
497
- return judge;
498
-
499
- }
500
-
501
-
502
-
503
- public boolean update(BookDto book) throws Exception {
504
-
505
- boolean judge = false;
506
-
507
- String sql = "UODATE T_BOOK SET BOOKTITLE = ?, BOOKTEXT = ?, AUTHOR = ?";
508
-
509
-
510
-
511
- String booktitle = book.getBooktitle();
512
-
513
- String booktext = book.getBooktext();
514
-
515
- String author = book.getAuthor();
516
-
517
-
518
-
519
- Connection conn = db.getConnection();
520
-
521
-
522
-
523
- PreparedStatement pStmt = conn.prepareStatement(sql);
524
-
525
- pStmt.setString(1, booktitle);
526
-
527
- pStmt.setString(2, booktext);
528
-
529
- pStmt.setString(3, author);
530
-
531
-
532
-
533
- int count = pStmt.executeUpdate();
534
-
535
-
536
-
537
- if (count == 1) {
538
-
539
- judge = true;
540
-
541
- }
542
-
543
- return judge;
544
-
545
- }
546
-
547
- }
583
+ String detail = null;
584
+
585
+
586
+
587
+ BookDto book = new BookDto(id, bookName, detail, author);
588
+
589
+ request.getAttribute("book");
590
+
591
+
592
+
593
+ bookName = book.getBooktitle();
594
+
595
+ author = book.getAuthor();
596
+
597
+ detail = book.getBooktext();
598
+
599
+ %>
600
+
601
+ <h2><%=bookName%></h2>
602
+
603
+ <h3>
604
+
605
+ 作者:<%=author%></h3>
606
+
607
+ <%=detail%>
608
+
609
+ <br>
610
+
611
+ <button onclick="history.back()">戻る</button>
612
+
613
+
614
+
615
+ </body>
616
+
617
+ </html>
548
618
 
549
619
  ```
550
-
551
- ```detail.jsp
552
-
553
- <%@ page language="java" contentType="text/html; charset=UTF-8"
554
-
555
- pageEncoding="UTF-8" import="java.io.*,java.util.*,java.text.*,dto.*"%>
556
-
557
- <!DOCTYPE html">
558
-
559
- <html>
560
-
561
- <head>
562
-
563
- <meta charset="UTF-8">
564
-
565
- <title>詳細画面</title>
566
-
567
- </head>
568
-
569
- <body>
570
-
571
- <%
572
-
573
- // リストデータをリクエストから取得
574
-
575
- int id = 0;
576
-
577
- String bookName = null;
578
-
579
- String author = null;
580
-
581
- String detail = null;
582
-
583
-
584
-
585
- BookDto book = new BookDto(id, bookName, detail, author);
586
-
587
- request.getAttribute("book");
588
-
589
-
590
-
591
- bookName = book.getBooktitle();
592
-
593
- author = book.getAuthor();
594
-
595
- detail = book.getBooktext();
596
-
597
- %>
598
-
599
- <h2><%=bookName%></h2>
600
-
601
- <h3>
602
-
603
- 作者:<%=author%></h3>
604
-
605
- <%=detail%>
606
-
607
- <br>
608
-
609
- <button onclick="history.back()">戻る</button>
610
-
611
-
612
-
613
- </body>
614
-
615
- </html>
616
-
617
- ```