public List<Book> findBooks(int page , int count) throws SQLException;
public int count() throws SQLException;
}
1
2
3
4
5
6
BookDaoImpl
public class BookDaoImpl implements BookDao{
private Connection conn = DBUtils.getConn();
@Override
public List<Book> findBooks(int page, int count) throws SQLException {
if(conn==null){
throw new NullPointerException("conn is null");
}
PreparedStatement ps = conn.prepareStatement("SELECT id,name,price,category,author,descs FROM tb_bookstore LIMIT ?,?");
if(ps==null){
throw new NullPointerException("ps is null");
}
ps.setInt(1, (page-1)*count);
ps.setInt(2, count);
ResultSet rs = ps.executeQuery();
if(rs==null){
throw new NullPointerException("rs is null");
}
List<Book> books = new ArrayList<>();
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt(1));
book.setName(rs.getString(2));
book.setPrice(rs.getDouble(3));
book.setCategory(rs.getString(4));
book.setAuthor(rs.getString(5));
book.setDescs(rs.getString(6));
books.add(book);
}
return books;
}
@Override
public int count() throws SQLException {
if(conn==null){
throw new NullPointerException("conn is null");
}
PreparedStatement ps = conn.prepareStatement("SELECT COUNT(*) FROM tb_bookstore");
if(ps==null){
throw new NullPointerException("ps is null");
}
ResultSet rs = ps.executeQuery();
if(rs==null){
throw new NullPointerException("rs is null");
}
int count = 0;
if (rs.next()) {
count = rs.getInt(1);
}
return count;
}
BookService service = new BookServiceImpl();
int currentPage=1;
int count=10;
String value = request.getParameter("page");
if(value!=null&&!"".equals(value)){
currentPage = Integer.parseInt(value);
}