读取数据库数据——分页展示

首先:导入c3po-config.xml和DataSourceUtils.java

1.新建index.jsp页面写入
	 分页展示所有电站
2.在domain中新建一个PageBean.java类,获取set .get方法,其中获取总页数是需要计算的,所以不需要用set方法,使用构造函数 private List list; //当前页内容 为一个泛型,需要将类型参数E添加至pageBean private int currPage; //当前页面 private int pageSize; //每页显示当地条数 private int totalCount; //总条数 private int totalPage; //总页数 public List getList() { return list; } public void setList(List list) { this.list = list; } public int getCurrPage() { return currPage; } public void setCurrPage(int currPage) { this.currPage = currPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } /** * 获取的总页数 * @param totalCount */ public int getTotalPage() { return (int) Math.ceil(totalCount*1.0/pageSize); } public PageBean(List list, int currPage, int pageSize, int totalCount) { super(); this.list = list; this.currPage = currPage; this.pageSize = pageSize; this.totalCount = totalCount; } 2.1在domain中新建一个StationInfo类,写入 package com.itheima.domain; public class StationInfo { private String stationId; private String stationName; public String getStationId() { return stationId; } public void setStationId(String stationId) { this.stationId = stationId; } public String getStationName() { return stationName; } public void setStationName(String stationName) { this.stationName = stationName; } public String getAdjustType() { return adjustType; } public void setAdjustType(String adjustType) { this.adjustType = adjustType; } public String getNormalZ() { return normalZ; } public void setNormalZ(String normalZ) { this.normalZ = normalZ; } public String getDeadZ() { return deadZ; } public void setDeadZ(String deadZ) { this.deadZ = deadZ; } public String getRiverId() { return riverId; } public void setRiverId(String riverId) { this.riverId = riverId; } private String adjustType; private String normalZ; private String deadZ; private String riverId; } 3.新建一个showStationByPageServlet的servlet,dopost()和doget()方法中写入 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //0.设置编码 //1.获取第一页 int currPage =Integer.parseInt(request.getParameter("currPage")); //固定每页的条数 int pageSize =5; //2.调用service完成分页,返回pagebean PageBean bean=null; try { bean = new StationService().showStationByPage(currPage,pageSize); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } //3.将pagebean放入request域中,请求转发station_page.jsp request.setAttribute("Station", bean); request.getRequestDispatcher("/station_page.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } 4.service包中新建StationService类,写入showStationByPage方法 /** * 分页查询 * @param currPage * @param pageSize * @return * @throws SQLException */ public PageBean showStationByPage(int currPage, int pageSize) throws SQLException { // TODO 自动生成的方法存根 //查询当前页数据 limit m,n StationDao dao=new StationDao(); List list=dao.findStationByPage(currPage,pageSize); int totalCount=dao.getCount(); //插叙总条数 return new PageBean<>(list, currPage, pageSize, totalCount); } 5.在Dao包中新建StationDao类,写入findStationByPage和getCount方法 //查询第几页数据 public List findStationByPage(int currPage, int pageSize) throws SQLException { // TODO 自动生成的方法存根 QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); String sql="select * from STATIONINFO limit ?,?"; return qr.query(sql, new BeanListHandler<>(StationInfo.class), (currPage-1)*pageSize,pageSize); } //获取总条数 public int getCount() throws SQLException { // TODO 自动生成的方法存根 QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); String sql="select count(*) from STATIONINFO"; return ((Long)qr.query(sql, new ScalarHandler())).intValue(); } private Object Long(Object query) { // TODO 自动生成的方法存根 return null; } 6.开始将数据传输送到station_page.jsp页面中,新建station_page.jsp页面,然后写入 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> My JSP 'station_page.jsp' starting page
stationIdstationNameadjustTypenormalZdeadZriverId
${p.stationId }${p.stationName }${p.adjustType }${p.normalZ }${p.deadZ }${p.riverId }
[首页] 【上一页】 ${n } ${n } ${n } ${n } ${n } ${n } 【下一页】 【尾页】 第${Station.currPage }页/共${Station.totalPage }页

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值