前提・実現したいこと
DBからテーブルを引っ張って、Javaで出力させたい(全件検索)のですが
表の中身を表示させることができません。
エラー等はコンソールに表示されていないです。
誤字等のケアレスミスであれば非常に申し訳ないのですが、これ以上どうすればいいか
分からないので、ご教授いただけますでしょうか。
該当のソースコード
beans
1package jp.co.sss.book.bean; 2 3import java.sql.Date; 4 5public class Book { 6 7 /** 書籍ID */ 8 private int bookId; 9 10 /** 書籍名 */ 11 private String bookName; 12 13 /** 著者 */ 14 private String author; 15 16 /** 発行日 */ 17 private Date publicationDate; 18 19 /** 在庫 */ 20 private int stock; 21 22 /** ジャンルID */ 23 private int genreId; 24 25 /** 26 * ブックIDを取得 27 * 28 * @return ブックID 29 */ 30 public int getBookId() { 31 return bookId; 32 } 33 34 /** 35 * ブックIDを保存 36 * 37 * @param bookId 38 * ブックID 39 */ 40 public void setBookId(int bookId) { 41 this.bookId = bookId; 42 } 43 44 public String getBookName() { 45 return bookName; 46 } 47 48 public void setBookName(String bookName) { 49 this.bookName = bookName; 50 } 51 52 public void setAuthor(String author) { 53 this.author = author; 54 } 55 56 public String getAuthor() { 57 return author; 58 } 59 60 public Date getPublicationDate() { 61 return publicationDate; 62 } 63 64 public void setPublicationDate(Date publicationDate) { 65 this.publicationDate = publicationDate; 66 } 67 68 public int getStock() { 69 return stock; 70 } 71 72 public void setStock(int stock) { 73 this.stock = stock; 74 } 75 76 public int getGenreId() { 77 return genreId; 78 } 79 80 public void setGenreId(int genreId) { 81 this.genreId = genreId; 82 } 83} 84
servlet1
1package jp.co.sss.book.dao; 2 3import java.sql.Connection; 4import java.sql.PreparedStatement; 5import java.sql.ResultSet; 6import java.sql.SQLException; 7import java.util.ArrayList; 8import java.util.List; 9 10import jp.co.sss.book.bean.Book; 11 12public class BookDao { 13 14 public static List<Book> findAll(){ 15 16 Connection con = null; 17 PreparedStatement ps = null; 18 ResultSet rs = null; 19 20 List<Book> bookList = new ArrayList<Book>(); 21 22 try { 23 con = DBManager.getConnection(); 24 ps = con.prepareStatement("SELECT * FROM book"); 25 rs = ps.executeQuery(); 26 while(rs.next()) { 27 Book book = new Book(); 28 book.setBookId(rs.getInt("book_id")); 29 book.setBookName(rs.getString("book_name")); 30 book.setAuthor(rs.getString("author")); 31 book.setPublicationDate(rs.getDate("publication_date")); 32 book.setStock(rs.getInt("stock")); 33 book.setGenreId(rs.getInt("genre_id")); 34 35 bookList.add(book); 36 } 37 }catch (SQLException e) { 38 e.printStackTrace(); 39 } finally { 40 DBManager.close(rs); 41 DBManager.close(ps); 42 DBManager.close(con); 43 } 44 return bookList; 45 } 46}
servlet2
1package jp.co.sss.book.servlet; 2 3import java.io.IOException; 4import java.util.List; 5 6import javax.servlet.ServletException; 7import javax.servlet.annotation.WebServlet; 8import javax.servlet.http.HttpServlet; 9import javax.servlet.http.HttpServletRequest; 10import javax.servlet.http.HttpServletResponse; 11 12import jp.co.sss.book.bean.Book; 13import jp.co.sss.book.dao.BookDao; 14 15 16 17 18/** 19 * Servlet implementation class BookList 20 */ 21@WebServlet("/BookList") 22public class BookList extends HttpServlet { 23 private static final long serialVersionUID = 1L; 24 25 /** 26 * @see HttpServlet#HttpServlet() 27 */ 28 public BookList() { 29 super(); 30 // TODO Auto-generated constructor stub 31 } 32 33 /** 34 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 35 */ 36 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 37 // TODO Auto-generated method stub 38 List<Book> bookList = BookDao.findAll(); 39 request.setAttribute("bookList", bookList); 40 request.getRequestDispatcher("/jsp/select/list.jsp"); 41 } 42} 43
jsp
1<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 3<!DOCTYPE html> 4<html lang="ja"> 5 <head> 6 <meta charset="UTF-8"> 7 <link href="<%=request.getContextPath()%>/css/style.css" rel="stylesheet" type="text/css" /> 8 <title>書籍一覧画面</title> 9 </head> 10 11 <body> 12 <%@include file="/jsp/common/header.jsp"%> 13 14 <div id="main"> 15 16 <table> 17 <tr> 18 <th>書籍ID</th> 19 <th>書籍名</th> 20 <th>筆者</th> 21 <th>発行日</th> 22 <th>在庫</th> 23 <th>ジャンル</th> 24 </tr> 25 26 <c:forEach var="book" items="${book}"> 27 28 <tr> 29 <td>${book.bookId}</td> 30 <td>${book.bookName}</td> 31 <td>${book.author}</td> 32 <td>${book.publicationId}</td> 33 <td>${book.stock}</td> 34 <td>${book.genreId}</td> 35 36 </tr> 37 38 </c:forEach> 39 </table> 40 </div> 41 <%@include file="/jsp/common/footer.jsp"%> 42 </body> 43</html>
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー