前提・実現したいこと
私、プログラミングの勉強を始めて一カ月弱の初学者です。
現在、Javaで掲示板システムを作っています。
掲示板に作成されたスレッドのタイトルをデータベースから取得して表示する一覧ページを作成したいです。
使用しているデータベースはpgAdmin4です。
発生している問題・エラーメッセージ
アプリケーションスコープにDAOで取り出したスレッドのタイトルを格納して、それをjspでスコープから取得して表示させたいのですが、表示させることができません。
データベースにデータは格納済みです。
自分でいろいろ調べたのですが、原因が解らないためここで質問させていただきました。
該当のソースコード
Java
ソースコード
package model;
import java.io.Serializable;
public class Thread implements Serializable {
private int id;
private int user_id;
private String thread_date;
private String title;
private boolean delete_flag;
public Thread() {} public Thread(String title) { this.title = title; } public Thread(int id,int user_id, String thread_date, String title,boolean delete_flag) { this.id = id; this.user_id = user_id; this.thread_date = thread_date; this.title = title; this.delete_flag = delete_flag; } public int getId() {return id;} public int getUser_id() {return user_id;} public String getThread_date() {return thread_date;} public String getTitle() {return title;} public boolean getDelete_flag() {return delete_flag;}
}
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.Thread;
public class ThreadListDAO {
public List<Thread> threadAll(){ Connection connection = null; List<Thread> threadList = new ArrayList<Thread>(); try { Class.forName("org.postgresql.Driver"); connection =DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "pass"); String selectSQL = "SELECT (title) FROM thread ORDER BY id DESC"; //スレッド一覧に表示する PreparedStatement selectStatement1 = connection.prepareStatement(selectSQL); ResultSet rs = selectStatement1.executeQuery(); while(rs.next()) { String title = rs.getString("title"); Thread thread = new Thread(title); threadList.add(thread); } }catch (SQLException e) { e.printStackTrace(); }catch(ClassNotFoundException e) { e.printStackTrace(); }finally { if(connection != null) { try { connection.close(); }catch(SQLException e) { e.printStackTrace(); } } } return threadList; }
}
package servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.ThreadListDAO;
import model.Thread;
@WebServlet("/ThreadList")
public class ThreadList extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ThreadListDAO tlDAO = new ThreadListDAO(); List<Thread> threadList = tlDAO.threadAll(); ServletContext application = this.getServletContext(); application.setAttribute("title", threadList); RequestDispatcher dispatcher = request.getRequestDispatcher("/Thread_List.jsp"); dispatcher.forward(request, response); }
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "model.Thread,model.Mc_user,java.util.List"%>
<%
List<Thread> threadList = (List<Thread>) application.getAttribute("title");
%>
<%! int i = 1;%>
<% for(Thread thread : threadList){ %>
<a href = "/MemberChat/Commentert">スレッド<%= i++ + thread.getTitle() %></a>
<button type = "button">削除</button><br>
<% } %>
試したこと
DAOの動作やjspの表示は問題ありません。
コンパイルエラーもありませんでした。
補足情報(FW/ツールのバージョンなど)
Eclipseを使っています。
回答1件
あなたの回答
tips
プレビュー