データベースからテーブルを持ってきてJSPで表示させたい(全件検索)のですが何度見直しても記述がうまくいかずエラーがでてしまいます。全レコードの情報を持ったlistを作成し格納したいのですがご教授いただければ幸いです。
servlet
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 10public class BookDAO { 11 public static List<Book> findAll(){ 12 Connection con = null; 13 14 PreparedStatement ps = null; 15 Book book = null; 16 List<Book> bookList = new ArrayList<>(); 17 try { 18 con = DBManager.getConnection(); 19 ps = con.prepareStatement("SELECT * FROM book WHERE book_id = ?"); 20 //ps.setString(1, bookId); 21 22 ResultSet rs = ps.executeQuery(); 23 24 while (rs.next()) { 25 book = new Book(); 26 book.setBookId(rs.getInt("book_id")); 27 book.setBookName(rs.getString("book_name")); 28 book.setAuthor(rs.getString("author")); 29 book.setPublicatoindate(rs.getDate("publicatoin_date")); 30 book.setStock(rs.getInt("stock")); 31 book.setGenreId(rs.getInt("genre_id")); 32 bookList.add(book); 33 } 34 } catch (SQLException e) { 35 e.printStackTrace(); 36 }finally{ 37 DBManager.close(ps, con); 38 } 39 return bookList; 40 } 41} 42 43```jsp 44
<%@ page contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ include file="../footer.jsp"%>
</body> </html> ``` ```bean package jp.co.sss.book.bean;import java.util.Date;
public class Book {
private int bookId; private String bookName; private String author; private Date publicatoindate; private int stock; private int genreId; public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public Date getPublicatoindate() { return publicatoindate; } public void setPublicatoindate(Date publicatoindate) { this.publicatoindate = publicatoindate; } public int getStock() { return stock; } public void setStock(int stock) { this.stock = stock; } public int getGenreId() { return genreId; } public void setGenreId(int genreId) { this.genreId = genreId; }
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/26 23:56