前提・実現したいこと
条件を入力し検索し、結果を表示させる。また、該当しない場合エラーメッセージを表示させる
発生している問題・エラーメッセージ
条件を入力してもすべてエラーメッセージの文が返ってくる
コンソール上にgenreが表示される
<%@ page contentType="text/html; charset=UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link href="css/style.css" rel="stylesheet" type="text/css" /> <title>ログイン画面</title> </head> <body> <%@ include file="../header.jsp"%> <div id="main"> 書籍一覧画面 <br /> <div style="display: inline-flex"> <%-- 書籍名の検索 --%> <form action="<%=request.getContextPath()%>/SelectAll" method="post"> 書籍名:<input type="text" name="bookName" /> <input type="submit" value="検索" /> </form> <%-- ジャンル名の検索 --%> <form action="<%=request.getContextPath()%>/Search" method="post"> ジャンル名:<select name="genre"> <option value="文学">文学</option> <option value="経済">経済</option> </select> <input type="submit" value="検索" /> </form> </div> <%-- 一覧表示 --%> <table border="1" style="border-collapse: collapse"> <tr> <th>書籍ID</th> <th>書籍名</th> <th>著者</th> <th>発行日</th> <th>在庫</th> <th>ジャンル名</th> </tr> <c:forEach var="bookUser" items="${bookList}"> <tr> <td>${bookUser.bookId}</td> <td>${bookUser.bookName}</td> <td>${bookUser.author}</td> <td>${bookUser.publicationDate}</td> <td>${bookUser.stock}</td> <td>${bookUser.genre.genreName}</td> </tr> </c:forEach> </table> <p class="error">${errMessage}</p> </div> <%@ include file="../footer.jsp"%> </body> </html>
DAO
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; 11import jp.co.sss.book.bean.Genre; 12 13public class BookDAO { 14 15 public static List<Book> findAll() { 16 Connection connection = null; 17 PreparedStatement preparedStatement = null; 18 List<Book> bookList = new ArrayList<Book>(); 19 try { 20 connection = DBManager.getConnection(); 21 preparedStatement = connection.prepareStatement("SELECT * FROM book b INNER JOIN genre g ON b.genre_id = g.genre_id ORDER BY book_id ASC" ); 22 // SQLの実行 23 ResultSet rs = preparedStatement.executeQuery(); 24 while (rs.next()) { 25 // テーブルからデータの取得 26 Book book = new Book(); 27 book.setBookId(rs.getInt("book_id")); 28 book.setBookName(rs.getString("book_name")); 29 book.setAuthor(rs.getString("author")); 30 book.setPublicationDate(rs.getDate("publication_date")); 31 book.setStock(rs.getInt("stock")); 32 Genre genre = new Genre(); 33 genre.setGenreId(rs.getInt("genre_id")); 34 genre.setGenreName(rs.getString("genre_name")); 35 book.setGenre(genre); 36 bookList.add(book); 37 38 } 39 }catch (SQLException e) { 40 e.printStackTrace(); 41 }finally { 42 DBManager.close(preparedStatement,connection); 43 } 44 return bookList; 45 46 } 47 48 public static List<Book> findByNameContains(String bookName) { 49 Connection con = null; 50 PreparedStatement ps = null; 51 List<Book> bookList = new ArrayList<Book>(); 52 try { 53 con = DBManager.getConnection(); 54 ps = con.prepareStatement("SELECT * FROM book b INNER JOIN genre g ON b.genre_id = g.genre_id WHERE book_name LIKE ? ORDER BY book_id ASC"); 55 ps.setString(1, "%" + bookName + "%"); 56 // SQLの実行 57 ResultSet rs = ps.executeQuery(); 58 // テーブルからデータの取得 59 while(rs.next()) { 60 Book book = new Book(); 61 book.setBookId(rs.getInt("book_id")); 62 book.setBookName(rs.getString("book_name")); 63 book.setAuthor(rs.getString("author")); 64 book.setPublicationDate(rs.getDate("publication_date")); 65 book.setStock(rs.getInt("stock")); 66 Genre genre = new Genre(); 67 genre.setGenreId(rs.getInt("genre_id")); 68 genre.setGenreName(rs.getString("genre_name")); 69 book.setGenre(genre); 70 bookList.add(book); 71 72 } 73 }catch (SQLException e) { 74 e.printStackTrace(); 75 }finally { 76 DBManager.close(ps,con); 77 } 78 return bookList; 79 80 } 81 82} 83
DAO
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; 11import jp.co.sss.book.bean.Genre; 12public class GenreDAO { 13 14 15 public static Genre findById(String genreId) { 16 Connection connection = null; 17 PreparedStatement preparedStatement = null; 18 Genre genre = null; 19 try { 20 connection = DBManager.getConnection(); 21 preparedStatement = connection.prepareStatement("SELECT * FROM genre WHERE genre_id = ?"); 22 preparedStatement.setString(1, genreId); 23 // SQLの実行 24 ResultSet rs = preparedStatement.executeQuery(); 25 while (rs.next()) { 26 genre = new Genre(); 27 genre.setGenreId(rs.getInt("genre_id")); 28 genre.setGenreName(rs.getString("genrename")); 29 } 30 }catch (SQLException e) { 31 e.printStackTrace(); 32 }finally { 33 DBManager.close(preparedStatement,connection); 34 } 35 return genre; 36 } 37 38 public static List<Genre> findByNameContains(String genreName) { 39 Connection con = null; 40 PreparedStatement ps = null; 41 List<Genre> genreList = new ArrayList<Genre>(); 42 try { 43 con = DBManager.getConnection(); 44 ps = con.prepareStatement("SELECT * FROM book b INNER JOIN genre g ON b.genre_id = g.genre_id WHERE genre_name LIKE ? ORDER BY book_id ASC"); 45 ps.setString(1, "%" + genreName + "%"); 46 // SQLの実行 47 ResultSet rs = ps.executeQuery(); 48 // テーブルからデータの取得 49 while(rs.next()) { 50 Genre genre = new Genre(); 51 genre.setGenreId(rs.getInt("genre_id")); 52 genre.setGenreName(rs.getString("genre_name")); 53 Book book = new Book(); 54 book.setBookId(rs.getInt("book_id")); 55 book.setBookName(rs.getString("book_name")); 56 book.setAuthor(rs.getString("author")); 57 book.setPublicationDate(rs.getDate("publication_date")); 58 book.setStock(rs.getInt("stock")); 59 genre.setBook(book); 60 genreList.add(genre); 61 62 } 63 }catch (SQLException e) { 64 e.printStackTrace(); 65 }finally { 66 DBManager.close(ps,con); 67 } 68 return genreList; 69 70 } 71} 72
servlet
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 * Servlet implementation class SelectAll 17 */ 18@WebServlet("/SelectAll") 19public class SelectAll extends HttpServlet { 20 21 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 22 23 String bookName = (String)request.getParameter("bookName"); 24 List<Book> bookList = BookDAO.findByNameContains(bookName); 25 26 27if (bookName.equals("bookName")) { 28 request.setAttribute("bookList", bookList); 29 request.getRequestDispatcher("/select/list.jsp").forward(request, response); 30 31 } else { 32 request.setAttribute("errMessage", "該当する書籍は存在しません。"); 33 request.getRequestDispatcher("/select/list.jsp").forward(request, response); 34 35 } 36 } 37 } 38 39
servlet
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.Genre; 13import jp.co.sss.book.dao.GenreDAO; 14 15/** 16 * Servlet implementation class Search 17 */ 18@WebServlet("/Search") 19public class Search extends HttpServlet { 20 21 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 22 23 String genreName = (String)request.getParameter("genre"); 24 List<Genre> genreList = GenreDAO.findByNameContains(genreName); 25 26 request.setAttribute("genreList", genreList); 27 request.getRequestDispatcher("/select/list.jsp").forward(request, response); 28 29 } 30} 31
試したこと
書籍名の検索はIF文を削除した場合には正常に処理された。
ジャンル名ではIF文がなくても動作しなかった
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー