Issue
I have a little problem, I'm just trying to display the data in my table, it was working 3 days ago but I don't know what I did .I'm putting the code snippets here. Thank you in advance for your help.
My Servlet
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>
Output :
<!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
I think the problem comes from the method "FindById" SORRY if it's not clear
Solution
Problem has been solved, I took the wrong ID can we close this subject ?
Answered By - smthrk