【发布时间】:2021-03-13 20:15:48
【问题描述】:
我有一个小问题,我只是想在我的表格中显示数据,它在 3 天前工作,但我不知道我做了什么。我将代码 sn-ps 放在这里。提前感谢您的帮助。
我的小服务程序
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
listBook(request, response);
}private void listBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Book> books = book.getAllBooks();
request.setAttribute("books", books);
this.getServletContext().getRequestDispatcher(VUE_LISTBOOK).forward(request, response);
}
BookImpl:
@Override
public List<Book> getAllbooks() {
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Book> books = new ArrayList<Book>();
try {
c = MysqlDaoFactory.getInstance().getConnection();
ps = c.prepareStatement(SQL_SELECT);
rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setIdBook(rs.getInt("id"));
book.setTitle(rs.getString("title"));
book.setIsbn(rs.getString("isbn"));
book.setEdition(rs.getString("edition"));
book.setPublicationDate(rs.getDate("publicationDate"));
book.setResume(rs.getString("resume"));
book.setNbPage(rs.getInt("nbPage"));
book.setBarcode(rs.getString("barcode"));
book.setImage(rs.getString("image"));
book.setCategory(category.findById(rs.getInt("id")));
books.add(book);
}
}
CategoryImpl :
private Category findById(String sql, int id) {
Connection connection = null;
Category category=null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = MysqlDaoFactory.getInstance().getConnection();
ps = connection.prepareStatement("SELECT * from category where id=?");
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
category = new Category();
int id2 = Integer.parseInt(rs.getString("id"));
String category1 = rs.getString("category");
category.setId(id2);
category.setCategory(category1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
MysqlConnectionFactory.closeConnection(connection);
MysqlConnectionFactory.closeResultSet(rs);
}
return category;
}
}
JSP:
<c:forEach items="${books}" var="books">
<tr>
<th><c:out value="${books.idBook}"/></th>
<th><c:out value="${books.title}"/></th>
<th><c:out value="${books.isbn}"/></th>
<th><c:out value="${books.edition}"/></th>
<th><c:out value="${books.publicationDate}"/></th>
<th><c:out value="${books.resume}"/></th>
<th><c:out value="${books.barcode}"/></th>
<th><c:out value="${books.image}"/></th>
<th><c:out value="${books.category.category}"/></th>
</tr>
</c:forEach>
输出:
<!DOCTYPE html>
<!-- meta -->
<meta charset="utf-8">
<tr>
<th>test</th>
<th>test</th>
<th>test</th>
<th>test</th>
<th>1111-05-05</th>
<th>test</th>
<th>test</th>
<th>test</th>
<th></th> ---> Empty
我认为问题来自“FindById”方法 如果不清楚,请见谅
【问题讨论】: