前提・実現したいこと
DBからテーブルを引っ張ってきて、JSPのほうで出力させたいのですが、
表の中身を表示させることが出来ません。
誤字等のケアレスミスであれば非常に申し訳ないのですが、お手上げ状態なので、ご教授いただけますでしょうか。
発生している問題・エラーメッセージ
at org.h2.message.DbException.getJdbcSQLException(DbException.java:451) at dao.PostDAO.findAll(PostDAO.java:25 at servlet.CreateServlet.doPost(CreateServlet.java:64) org.h2.jdbc.JdbcSQLSyntaxErrorException: 列 "ID" が見つかりません Column "ID" not found; SQL statement: SELECT MESSAGE, IMAGE, VIDEO FROM POSTS ORDER BY ID DESC [42122-200] at org.h2.message.DbException.getJdbcSQLException(DbException.java:453) at org.h2.message.DbException.getJdbcSQLException(DbException.java:429) at org.h2.message.DbException.get(DbException.java:205) at org.h2.message.DbException.get(DbException.java:181) at org.h2.expression.ExpressionColumn.getColumnException(ExpressionColumn.java:163) at org.h2.expression.ExpressionColumn.optimize(ExpressionColumn.java:145) at org.h2.command.dml.Select.prepare(Select.java:1206) at org.h2.command.Parser.prepareCommand(Parser.java:744) at org.h2.engine.Session.prepareLocal(Session.java:657) at org.h2.server.TcpServerThread.process(TcpServerThread.java:278) at org.h2.server.TcpServerThread.run(TcpServerThread.java:183) at java.lang.Thread.run(Unknown Source) at org.h2.message.DbException.getJdbcSQLException(DbException.java:451) at org.h2.engine.SessionRemote.done(SessionRemote.java:607) at org.h2.command.CommandRemote.prepare(CommandRemote.java:85) at org.h2.command.CommandRemote.<init>(CommandRemote.java:51) at org.h2.engine.SessionRemote.prepareCommand(SessionRemote.java:477) at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1292) at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:77) at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:349) at dao.PostDAO.findAll(PostDAO.java:25) at model.GetPostListLogic.execute(GetPostListLogic.java:11) at servlet.CreateServlet.doGet(CreateServlet.java:32) at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
PostDAO
1package dao; 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 11import model.Post; 12 13public class PostDAO { 14 15 private final String JDBC_URL = "jdbc:h2:tcp://localhost/~/posters"; 16 private final String DB_USER = "sa"; 17 private final String DB_PASS = ""; 18 19 public List<Post> findAll() { 20 List<Post> postList = new ArrayList<>(); 21 22 23 try ( Connection conn = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASS)) { 24 String sql = "SELECT MESSAGE, IMAGE, VIDEO FROM POSTS ORDER BY ID DESC"; 25 PreparedStatement pStmt = conn.prepareStatement(sql); 26 27 ResultSet rs = pStmt.executeQuery(); 28 29 while(rs.next()) { 30 String message = rs.getString("MESSAGE"); 31 String image = rs.getString("IMAGE"); 32 String video = rs.getString("VIDEO"); 33 Post post = new Post(message, image, video); 34 postList.add(post); 35 } 36 }catch (SQLException e) { 37 e.printStackTrace(); 38 return null; 39 } 40 return postList; 41 } 42 43 public void setData(String message, String image, String video) { 44 45 46 try(Connection conn = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PASS)){ 47 48 // Statement stmt = conn.createStatement(); 49 String sql = "INSERT INTO POSTS( MESSAGE, IMAGE, VIDEO) VALUES(?, ?, ?) "; 50 PreparedStatement pStmt = conn.prepareStatement(sql); 51 pStmt.setString(1, message); 52 pStmt.setString(2, image); 53 pStmt.setString(3, video); 54 pStmt.executeUpdate(); 55 56 57 }catch (SQLException e) { 58 System.out.println("SQLException:" + e.getMessage()); 59 } 60 } 61} 62
top.jsp
top.jsp
1<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 5<!DOCTYPE html> 6<html> 7<head> 8<meta charset="UTF-8"> 9<title>Posters</title> 10</head> 11<body> 12<p>ようこそ<c:out value="${userId }" />さん</p> 13<c:forEach var="post" items="${postList }"> 14 <p><c:out value="${post.message}"></c:out>:<c:out value="${post.image }"></c:out><c:out value="${post.video }"></c:out></p> 15</c:forEach> 16 17<a href="/Posters/NewServlet">投稿する</a> 18<a href="/Posters/IndexServlet">ログアウト</a> 19 20 21</body> 22</html>
CreateServlet.java
CreateServlet.java
1package servlet; 2 3import java.io.IOException; 4import java.util.List; 5 6import javax.servlet.RequestDispatcher; 7import javax.servlet.ServletException; 8import javax.servlet.annotation.WebServlet; 9import javax.servlet.http.HttpServlet; 10import javax.servlet.http.HttpServletRequest; 11import javax.servlet.http.HttpServletResponse; 12 13import dao.PostDAO; 14import model.GetPostListLogic; 15import model.Post; 16 17@WebServlet("/CreateServlet") 18public class CreateServlet extends HttpServlet { 19 private static final long serialVersionUID = 1L; 20 21 22 public CreateServlet() { 23 super(); 24 // TODO Auto-generated constructor stub 25 } 26 protected void doGet(HttpServletRequest request, HttpServletResponse response) 27 throws ServletException, IOException { 28 29 30 31 GetPostListLogic getPostListLogic = new GetPostListLogic(); 32 List<Post> postList = getPostListLogic.execute(); 33 request.setAttribute("postList", postList); 34 35 36 37 RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/top.jsp"); 38 dispatcher.forward(request, response); 39 40 } 41 42 43 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 44 response.getWriter().append("Served at: ").append(request.getContextPath()); 45 46 47 String message = request.getParameter("message"); 48 String image = request.getParameter("image"); 49 String video = request.getParameter("video"); 50 Post post = new Post(message, image, video); 51 52 53 PostDAO dao = new PostDAO(); 54 dao.setData(message, image, video); 55 56 GetPostListLogic getPostListLogic = new GetPostListLogic(); 57 List<Post> postList = getPostListLogic.execute(); 58 request.setAttribute("postList", postList); 59 60 61// RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/top.jsp"); 62// dispatcher.forward(request, response); 63 64 65 66 String forward = "/Posters/CreateServlet"; 67 response.sendRedirect(forward); 68 69 } 70} 71
試したこと
H2DBにはUSERS(user_id,pass,email,name)、POSTS(message,image,video)のテーブルを作成。
両方ともDBへの保存はできています。
DBのデータをSELECTで探しPostDAOのfindAllメソッドで引き出しJSPでEL式で呼び出そうとしましたが、ブラウザにはUSERSテーブルのデータは表示されますが、POSTSテーブルのデータは表示されません。
top.jspに関係のあるservletでPostDAOを使うようにしてはと思い、CreateServlet.javaのdoGetに記載をして試しましたが変わりませんでした。
補足情報
eclipse
tomcat9
DB:H2
よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/20 07:35