質問編集履歴

4

追記情報

2016/08/02 06:09

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -755,3 +755,21 @@
755
755
 
756
756
 
757
757
  ```
758
+
759
+ 追記4
760
+
761
+
762
+
763
+ Main.javaとTest.javaではDAOのfindAll()内の処理が異なるようです。
764
+
765
+ 1 : Main.java、Test.javaはともにClass.forName( DRIVER_NAME );までは読み込む
766
+
767
+ 2 : Main.javaはその後finally{}へ飛ぶ
768
+
769
+ 3 : Test.javaは順調にcon = DriverManager.getConnection( JDBC_URL, DB_USER, DB_PASS );を読み込む
770
+
771
+ 4 : 仮にClass.forName()でエラーを出していた場合、ClassNotFoundExceptionでエラーを捕捉していない
772
+
773
+
774
+
775
+ もはやソースコード上の問題なのか不明です。

3

追記

2016/08/02 06:09

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -549,3 +549,209 @@
549
549
 
550
550
 
551
551
  こう見ると、そもそもサーブレットクラスのMain.javaで取得ができていないようですね、、。
552
+
553
+
554
+
555
+ ---
556
+
557
+ 追記3
558
+
559
+
560
+
561
+ DAOクラスのfindAll()までデバッグを行いました。
562
+
563
+
564
+
565
+ 結果、Test.javaではfindAll()でDBからの情報を取得し、しっかりと返り値を返しています。しかし、Main.javaではfindAll()ではtry{}が全てスキップしており、finally{}のみ実行されていました。(コードは最後に書きます)
566
+
567
+
568
+
569
+ 関係しているファイルは最初に記載した通りで、今回の流れではDBにSELECT文しか送っておらず、かなり簡単なもののはずですが、どこがおかしいのか、、、。
570
+
571
+
572
+
573
+ ```java
574
+
575
+ package dao;
576
+
577
+
578
+
579
+ import java.sql.Connection;
580
+
581
+ import java.sql.DriverManager;
582
+
583
+ import java.sql.PreparedStatement;
584
+
585
+ import java.sql.ResultSet;
586
+
587
+ import java.sql.SQLException;
588
+
589
+ import java.util.ArrayList;
590
+
591
+ import java.util.List;
592
+
593
+
594
+
595
+ import model.Mutter;
596
+
597
+
598
+
599
+ public class MutterDAO{
600
+
601
+ private final String DRIVER_NAME = "com.mysql.jdbc.Driver";
602
+
603
+ private final String JDBC_URL = "jdbc:mysql://localhost:8889/docoTsubu";
604
+
605
+ private final String DB_USER = "○○○○○○○";
606
+
607
+ private final String DB_PASS = "○○○○○○○";
608
+
609
+
610
+
611
+ public List< Mutter > findAll(){
612
+
613
+ Connection con = null;
614
+
615
+ List< Mutter > mutterList = new ArrayList<>();
616
+
617
+
618
+
619
+ try {
620
+
621
+ Class.forName( DRIVER_NAME );
622
+
623
+ con = DriverManager.getConnection( JDBC_URL, DB_USER, DB_PASS );
624
+
625
+
626
+
627
+ // SQLの準備
628
+
629
+ String sql = "SELECT id, name, text FROM mutter ORDER BY id DESC";
630
+
631
+ PreparedStatement pstmt = con.prepareStatement( sql );
632
+
633
+ ResultSet rs = pstmt.executeQuery();
634
+
635
+
636
+
637
+ while( rs.next() ) {
638
+
639
+ int id = rs.getInt( "id" );
640
+
641
+ String userName = rs.getString( "name" );
642
+
643
+ String text = rs.getString( "text" );
644
+
645
+ Mutter mutter = new Mutter( id, userName, text );
646
+
647
+ mutterList.add( mutter );
648
+
649
+ }
650
+
651
+
652
+
653
+ } catch ( ClassNotFoundException e ) {
654
+
655
+ e.printStackTrace();
656
+
657
+ } catch ( SQLException e ) {
658
+
659
+ e.printStackTrace();
660
+
661
+ } finally {
662
+
663
+ if( con != null ) {
664
+
665
+ try {
666
+
667
+ con.close();
668
+
669
+
670
+
671
+ } catch ( SQLException e ) {
672
+
673
+ e.printStackTrace();
674
+
675
+ }
676
+
677
+ }
678
+
679
+ }
680
+
681
+
682
+
683
+ return mutterList;
684
+
685
+ }
686
+
687
+
688
+
689
+ public boolean create( Mutter mutter ){
690
+
691
+ Connection con = null;
692
+
693
+ try {
694
+
695
+ con = DriverManager.getConnection( JDBC_URL, DB_USER, DB_PASS );
696
+
697
+
698
+
699
+ String sql = "INSERT INTO mutter( name, text ) VALUES( ?, ? )";
700
+
701
+ PreparedStatement pstmt = con.prepareStatement( sql );
702
+
703
+ pstmt.setString( 1, mutter.getUserName() );
704
+
705
+ pstmt.setString( 2, mutter.getText() );
706
+
707
+
708
+
709
+ int result = pstmt.executeUpdate();
710
+
711
+
712
+
713
+ if( result != 1 ) {
714
+
715
+ return false;
716
+
717
+ }
718
+
719
+
720
+
721
+ } catch ( SQLException e ) {
722
+
723
+ e.printStackTrace();
724
+
725
+ } finally {
726
+
727
+ if( con != null ) {
728
+
729
+ try {
730
+
731
+ con.close();
732
+
733
+
734
+
735
+ } catch ( SQLException e ) {
736
+
737
+ e.printStackTrace();
738
+
739
+ }
740
+
741
+ }
742
+
743
+ }
744
+
745
+
746
+
747
+ return true;
748
+
749
+ }
750
+
751
+
752
+
753
+ }
754
+
755
+
756
+
757
+ ```

2

デバッグ結果追記

2016/07/31 06:46

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -427,3 +427,125 @@
427
427
  コード
428
428
 
429
429
  ```
430
+
431
+
432
+
433
+ ---
434
+
435
+ 追記2
436
+
437
+
438
+
439
+ 下記にデバッグ結果を書きます
440
+
441
+
442
+
443
+ ```java
444
+
445
+ protected void doGet( HttpServletRequest request, HttpServletResponse response )
446
+
447
+ throws ServletException, IOException{
448
+
449
+
450
+
451
+ GetMutterListLogic gm = new GetMutterListLogic();
452
+
453
+ List< Mutter > mutterList = gm.execute();
454
+
455
+ request.setAttribute( "mutterList", mutterList );
456
+
457
+ // ↑ブレークポイント
458
+
459
+ }
460
+
461
+ /*
462
+
463
+ * mutterList : ArrayList<E>
464
+
465
+ * elementData : Object[0]
466
+
467
+ * modCount : 0
468
+
469
+ * size : 0
470
+
471
+ *
472
+
473
+ */
474
+
475
+
476
+
477
+ ```
478
+
479
+
480
+
481
+ ```java
482
+
483
+ package test;
484
+
485
+
486
+
487
+ import java.util.List;
488
+
489
+
490
+
491
+ import model.GetMutterListLogic;
492
+
493
+ import model.Mutter;
494
+
495
+
496
+
497
+ public class Test{
498
+
499
+
500
+
501
+ public static void main( String[] args ){
502
+
503
+ GetMutterListLogic gm = new GetMutterListLogic();
504
+
505
+ List< Mutter > mutterLista = gm.execute();
506
+
507
+
508
+
509
+ for( Mutter mutter : mutterLista ) {
510
+
511
+ // ↑ブレークポイント
512
+
513
+ System.out.print( mutter.getUserName() + ":" );
514
+
515
+ System.out.println( mutter.getText() );
516
+
517
+ }
518
+
519
+
520
+
521
+ }
522
+
523
+
524
+
525
+ }
526
+
527
+
528
+
529
+ /*
530
+
531
+ *
532
+
533
+ * mutterList
534
+
535
+ * elementData : Object[10]
536
+
537
+ * modCount : 2
538
+
539
+ * size : 2
540
+
541
+ *
542
+
543
+ */
544
+
545
+ ```
546
+
547
+
548
+
549
+
550
+
551
+ こう見ると、そもそもサーブレットクラスのMain.javaで取得ができていないようですね、、。

1

追加情報

2016/07/31 06:16

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -273,3 +273,157 @@
273
273
 
274
274
 
275
275
  ```
276
+
277
+
278
+
279
+ ---
280
+
281
+ 追記情報です。
282
+
283
+ ```java
284
+
285
+ package servlet;
286
+
287
+
288
+
289
+ import java.io.IOException;
290
+
291
+ import java.util.List;
292
+
293
+
294
+
295
+ import javax.servlet.RequestDispatcher;
296
+
297
+ import javax.servlet.ServletException;
298
+
299
+ import javax.servlet.annotation.WebServlet;
300
+
301
+ import javax.servlet.http.HttpServlet;
302
+
303
+ import javax.servlet.http.HttpServletRequest;
304
+
305
+ import javax.servlet.http.HttpServletResponse;
306
+
307
+ import javax.servlet.http.HttpSession;
308
+
309
+
310
+
311
+ import model.GetMutterListLogic;
312
+
313
+ import model.Mutter;
314
+
315
+ import model.PostMutterLogic;
316
+
317
+ import model.User;
318
+
319
+
320
+
321
+ @WebServlet( "/Main" )
322
+
323
+ public class Main extends HttpServlet{
324
+
325
+ private static final long serialVersionUID = 1L;
326
+
327
+
328
+
329
+ protected void doGet( HttpServletRequest request, HttpServletResponse response )
330
+
331
+ throws ServletException, IOException{
332
+
333
+
334
+
335
+ // DBからつぶやきリストを取得→リクエストスコープに保存
336
+
337
+ GetMutterListLogic gm = new GetMutterListLogic();
338
+
339
+ List< Mutter > mutterList = gm.execute();
340
+
341
+ request.setAttribute( "mutterList", mutterList );
342
+
343
+
344
+
345
+ // ここでセッションIDをキーにインスタンスを取得
346
+
347
+ HttpSession session = request.getSession();
348
+
349
+ User loginUser = ( User ) session.getAttribute( "loginUser" );
350
+
351
+
352
+
353
+ if( loginUser == null ) {
354
+
355
+ response.sendRedirect( "/docoTsubu/" );
356
+
357
+ } else {
358
+
359
+ RequestDispatcher dispatcher = request.getRequestDispatcher( "/WEB-INF/jsp/main.jsp" );
360
+
361
+ dispatcher.forward( request, response );
362
+
363
+ }
364
+
365
+
366
+
367
+ }
368
+
369
+
370
+
371
+ protected void doPost( HttpServletRequest request, HttpServletResponse response )
372
+
373
+ throws ServletException, IOException{
374
+
375
+
376
+
377
+ String text = request.getParameter( "text" );
378
+
379
+
380
+
381
+ if( text != null && text.length() != 0 ) {
382
+
383
+ HttpSession session = request.getSession();
384
+
385
+ User loginUser = ( User ) request.getAttribute( "loginUser" );
386
+
387
+
388
+
389
+ Mutter mutter = new Mutter( loginUser.getName(), text );
390
+
391
+ PostMutterLogic pm = new PostMutterLogic();
392
+
393
+ pm.execute( mutter );
394
+
395
+
396
+
397
+ } else {
398
+
399
+ request.setAttribute( "errorMsg", "つぶやきが入力されていません" );
400
+
401
+ }
402
+
403
+
404
+
405
+ GetMutterListLogic gm = new GetMutterListLogic();
406
+
407
+ List< Mutter > mutterList = gm.execute();
408
+
409
+ request.setAttribute( "mutterList", mutterList );
410
+
411
+
412
+
413
+ RequestDispatcher dispatcher = request.getRequestDispatcher( "/WEB-INF/jsp/main.jsp" );
414
+
415
+ dispatcher.forward( request, response );
416
+
417
+
418
+
419
+ }
420
+
421
+
422
+
423
+ }
424
+
425
+
426
+
427
+ コード
428
+
429
+ ```