前提
入力画面で「名前」と「コメント」を入力すると、
それらがデータベースに格納されていくというアプリを学習のために作っています。
発生している問題・エラーメッセージ
特にエラーが出ているわけではないと思うのですが、
名前等を入力してもデータベースに格納されません。
アプリの概要としては、
「InputComment」で入力された名前等の情報を
「SaveComment」→「SaveCommentBL」→「CommentDao」と受け渡し、
データベースに格納するというものです。
該当のソースコード
java
1package work; 2 3import java.io.IOException; 4import java.io.PrintWriter; 5 6import javax.servlet.ServletException; 7import javax.servlet.http.HttpServlet; 8import javax.servlet.http.HttpServletRequest; 9import javax.servlet.http.HttpServletResponse; 10 11 12public class InputComment extends HttpServlet { 13 private static final long serialVersionUID = 1L; 14 15 public InputComment() { 16 super(); 17 } 18 19 protected void doGet(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21 22 response.setContentType("text/html;charset=UTF-8"); 23 24 PrintWriter out = response.getWriter(); 25 26 out.println("<html> "); 27 out.println("<head> "); 28 out.println(" <title>コメント入力</title> "); 29 out.println("</head> "); 30 out.println("<body> "); 31 out.println(" <h2>コメント入力画面</h2> "); 32 out.println(" <form action=\"SaveComment\" method=\"post\"> "); 33 out.println(" <p>名前: "); 34 out.println(" <input type=\"text\" name=\"NAME\" maxlength = \"20\"> "); 35 out.println(" </p> "); 36 out.println(" <p>コメント:<br> "); 37 out.println(" <textarea name=\"MESSAGE\" rows=\"4\" cols=\"50\" maxlength = \"250\"></textarea> "); 38 out.println(" </p> "); 39 out.println(" <input type=\"submit\" value=\"送信\"> "); 40 out.println(" </form> "); 41 out.println(" <br> "); 42 out.println(" <a href=\"ShowAllComment\">コメント一覧画面へ</a> "); 43 out.println("</body> "); 44 out.println("</html> "); 45 46 } 47 48 protected void doPost(HttpServletRequest request, HttpServletResponse response) 49 throws ServletException, IOException { 50 doGet(request, response); 51 } 52 53}
java
1package work; 2 3import java.io.IOException; 4 5import javax.servlet.ServletException; 6import javax.servlet.http.HttpServlet; 7import javax.servlet.http.HttpServletRequest; 8import javax.servlet.http.HttpServletResponse; 9 10 11public class SaveComment extends HttpServlet { 12 private static final long serialVersionUID = 1L; 13 14 public SaveComment() { 15 super(); 16 } 17 18 protected void doGet(HttpServletRequest request, HttpServletResponse response) 19 throws ServletException, IOException { 20 doPost(request, response); 21 } 22 23 protected void doPost(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 26 response.setContentType("text/html;charset=UTF-8"); 27 28 request.setCharacterEncoding("UTF-8"); 29 30 boolean succesFlg = true; 31 32 String name = request.getParameter("NAME"); 33 String message = request.getParameter("MESSAGE"); 34 35 CommentsDto dto = new CommentsDto(); 36 dto.setName( name ); 37 dto.setMessage( message ); 38 39 SaveCommentBL logic = new SaveCommentBL(); 40 succesFlg = true; 41 42 if (succesFlg) { 43 44 response.sendRedirect("htmls/finish.html"); 45 46 } else { 47 48 response.sendRedirect("htmls/error.html"); 49 50 } 51 } 52} 53
java
1package work; 2 3 4public class SaveCommentBL { 5 6 public boolean executeInsertSurvey(CommentsDto dto) { 7 8 boolean succesInsert = false ; 9 10 CommentsDao dao = new CommentsDao(); 11 succesInsert = dao.doInsert(dto); 12 13 return succesInsert; 14 } 15 16} 17
java
1package work; 2 3import java.sql.Connection; 4import java.sql.DriverManager; 5import java.sql.PreparedStatement; 6import java.sql.ResultSet; 7import java.sql.SQLException; 8import java.util.ArrayList; 9import java.util.List; 10 11 12public class CommentsDao { 13 14 String DRIVER_NAME = "com.mysql.cj.jdbc.Driver"; 15 16 String JDBC_URL = "jdbc:mysql://localhost/test_db?characterEncoding=UTF-8&serverTimezone=JST&useSSL=false"; 17 18 String USER_ID = "test_user"; 19 20 String USER_PASS = "test_pass"; 21 22 23 public boolean doInsert(CommentsDto dto) { 24 25 try { 26 Class.forName(DRIVER_NAME); 27 } catch (ClassNotFoundException e) { 28 e.printStackTrace(); 29 } 30 31 Connection con = null ; 32 PreparedStatement ps = null ; 33 34 boolean isSuccess = true ; 35 36 try { 37 38 con = DriverManager.getConnection(JDBC_URL, USER_ID, USER_PASS); 39 40 con.setAutoCommit(false); 41 42 StringBuffer buf = new StringBuffer(); 43 buf.append(" INSERT INTO comments ( "); 44 buf.append(" NAME, "); 45 buf.append(" MESSAGE, "); 46 buf.append(") VALUES ( "); 47 buf.append(" ?, "); 48 buf.append(" ? "); 49 buf.append(") "); 50 51 ps = con.prepareStatement(buf.toString()); 52 53 ps.setString( 1, dto.getName() ); 54 ps.setString( 2, dto.getMessage() ); 55 56 ps.executeUpdate(); 57 58 } catch (SQLException e) { 59 60 e.printStackTrace(); 61 62 isSuccess = false ; 63 64 } finally { 65 66 if(isSuccess){ 67 try { 68 con.commit(); 69 } catch (SQLException e) { 70 e.printStackTrace(); 71 } 72 73 }else{ 74 try { 75 con.rollback(); 76 } catch (SQLException e) { 77 e.printStackTrace(); 78 } 79 } 80 81 if (ps != null) { 82 try { 83 ps.close(); 84 } catch (SQLException e) { 85 e.printStackTrace(); 86 } 87 } 88 89 if (con != null) { 90 try { 91 con.close(); 92 } catch (SQLException e) { 93 e.printStackTrace(); 94 } 95 } 96 97 } 98 99 return isSuccess; 100 } 101 102 103 104 105 public List<CommentsDto> doSelect() { 106 107 try { 108 Class.forName(DRIVER_NAME); 109 } catch (ClassNotFoundException e) { 110 e.printStackTrace(); 111 } 112 113 Connection con = null ; 114 PreparedStatement ps = null ; 115 ResultSet rs = null ; 116 117 List<CommentsDto> dtoList = new ArrayList<CommentsDto>(); 118 119 try { 120 121 con = DriverManager.getConnection(JDBC_URL, USER_ID, USER_PASS); 122 123 StringBuffer buf = new StringBuffer(); 124 buf.append("SELECT "); 125 buf.append(" NAME, "); 126 buf.append(" MESSAGE "); 127 buf.append(" FROM "); 128 buf.append(" COMMENTS; "); 129 130 ps = con.prepareStatement(buf.toString()); 131 rs = ps.executeQuery(); 132 133 while (rs.next()) { 134 CommentsDto dto = new CommentsDto(); 135 dto.setName(rs.getString( "NAME" ) ); 136 dto.setMessage(rs.getString( "MESSAGE" ) ); 137 dtoList.add(dto); 138 } 139 140 } catch (SQLException e) { 141 e.printStackTrace(); 142 } finally { 143 144 if (rs != null) { 145 try { 146 rs.close(); 147 } catch (SQLException e) { 148 e.printStackTrace(); 149 } 150 } 151 152 if (ps != null) { 153 try { 154 ps.close(); 155 } catch (SQLException e) { 156 e.printStackTrace(); 157 } 158 } 159 160 if (con != null) { 161 try { 162 con.close(); 163 } catch (SQLException e) { 164 e.printStackTrace(); 165 } 166 } 167 } 168 169 return dtoList; 170 } 171 172} 173
試したこと
- 他のコードであれば、問題なくデータベースにデータ格納できているので、環境構築は問題ないと思います。
- mysqlのパスワードはあっています。
- DAOが怪しいと思っており、Eclipseのデバッグを試しましたが、404エラーが表示されます。(そもそもウェブアプリのデバッグの方法がこれで正しいのかわかっていません。。。)
補足情報(FW/ツールのバージョンなど)
添付のコードは既存の正常に動作するアプリを修正して作成したものです。
元々はバリデーションチェックなど様々な機能がありましたが、
ひとまず最小構成のアプリを作ってみようといろいろと機能を削りました。
(if文にtrueが渡され、分岐が行われないようになっていたりするのはそのような経緯のためです。)
また、初学者のため質問のために必要な情報がこれで足りているかどうかわかりません。
不足があればご指摘いただけたらと思います。
回答1件
あなたの回答
tips
プレビュー