jsp+Javabean+servlet实现分页查询

今天小编那写了个分页请大家参考

dao:对数据库进行操作
public List<Product> findProduct(int start, int pageSize)throws SQLException {
		//用数据库连接池初始化runner对象
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		//sql进行分页查询
		String sql = "select * from product limit ?,?";
		return qr.query(sql, new BeanListHandler<Product>(Product.class),
start, pageSize);
	}
// 查询商品的总数
	public int findTotalCount() throws SQLException {
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select count(*) from product";
		return ((Long) qr.query(sql, new ScalarHandler())).intValue();
	}
service:进行逻辑判断
private ProductDao pd = new ProductDao();


	public List<Product> findProduct(int currentPage, int pageSize) {


		try {
			//调用dao层方法查出当前页码号即(currentPage - 1) * pageSize
			return pd.findProduct((currentPage - 1) * pageSize, pageSize);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
//用pagesize做参数计算出最大页码号
	public int getMaxPage(int pageSize) {
		int totalCount;
		try {
			totalCount = pd.findTotalCount();
			//运用三目运算 如果总数/页数有余就让页码号加一否则不加
			return totalCount % pageSize > 0 ? totalCount / pageSize + 1
					: totalCount / pageSize;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
servlet:与前台进行交互
private ProductService ps = new ProductService();
	private final int pageSize = 10;

	public void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int currentPage = 1;
		if (request.getParameter("currentPage") != null) {
			currentPage = Integer.parseInt(request.getParameter("currentPage"));
		}
		int maxPage = ps.getMaxPage(pageSize);
		List<Product> list = ps.findProduct(currentPage, pageSize);
		request.setAttribute("list", list);
		request.setAttribute("currentPage", currentPage);
		request.setAttribute("maxPage", maxPage);
		request.getRequestDispatcher("/admin/product/list.jsp").forward(
				request, response);
	}
前台遍历数据:
<c:forEach items="${list}" var="product">
<tr οnmοuseοver="this.style.backgroundColor = 'white'"
οnmοuseοut="this.style.backgroundColor = '#F5FAFE';">
<td style="CURSOR: hand; HEIGHT: 22px" align="center"width="18%">${product.pid}</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%">
<img width="40" height="45" src="${pageContext.request.contextPath}/${product.pimage}"></td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%">${product.pname}</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%">${product.shop_price}</td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%">${product.is_hot}</td>
<td align="center" style="HEIGHT: 22px">
<ahref="${ pageContext.request.contextPath }/product?method=toUpdate&&pid=${product.pid}">
<imgsrc="${pageContext.request.contextPath}/images/i_edit.gif"border="0" style="CURSOR: hand">
</a></td>
<td align="center" style="HEIGHT: 22px">
<a href="${pageContext.request.contextPath}/product?method=delete&&pid=${product.pid}"> <img
src="${pageContext.request.contextPath}/images/i_del.gif"width="16" height="16" border="0" 
style="CURSOR: hand">
</a></td>
</tr>
</c:forEach> 
Thank you for reading


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值