//添加Pager类
package Model; import java.util.List; public class Pager{ //数据总条数? private int totalRecord; //每一页显示的数据条数 private int pageSize; //页码数 private int pageIndex; //总页数 private int totalPage; //查询到的数据的集合 private List datas; public int getTotalRecord() { return totalRecord; } public void setTotalRecord(int totalRecord) { this.totalRecord = totalRecord; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPageIndex() { return pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public List getDatas() { return datas; } public void setDatas(List datas) { this.datas = datas; } }
Dao类
public Pagerload(String content, int pageIndex, int pageSize) { Pager pager = new Pager (); Connection connection = DBUtil.getConnection(); //创建语句传输对象 PreparedStatement preparedStatement = null; ResultSet rs = null; //准备sql语句 String sql = "select * from tea_all_info "; //数据总条数 String sqlCount = "select count(*) from tea_all_info"; //集合中只能放入user对象 List tais = new ArrayList (); Tea_all_info tai = null; try { if (pageIndex <= 0) { pageIndex = 1; } int start = (pageIndex-1)*pageSize; // if (content != null && !"".equals(content)) { // sql += " where username like '%" +content+"%'" + "or name like '%" +content + "%'"; // sqlCount += " where username like '%" +content+"%'" + "or name like '%" +content + "%'"; // } System.out.println(sql); preparedStatement = connection.prepareStatement(sqlCount); rs = preparedStatement.executeQuery(); //总记录数 int totalRecord = 0; while(rs.next()) { totalRecord = rs.getInt(1); } //总页数 int totalPage = totalRecord%pageSize == 0 ? totalRecord/pageSize : totalRecord/pageSize + 1; //加分页 sql += " limit ?,?"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1, start); preparedStatement.setInt(2, pageSize); rs = preparedStatement.executeQuery(); //System.out.println(sql+"模糊查询"); while(rs.next()) { tai = new Tea_all_info(); tai.setId(rs.getInt("id")); tai.setTea_name(rs.getString("tea_name")); tai.setTea_brand(rs.getString("tea_brand")); tai.setTea_store(rs.getString("tea_store")); tai.setTea_img(rs.getString("tea_img")); tai.setTea_big_type(rs.getString("tea_big_type")); tai.setTea_comment_count(rs.getString("tea_comment_count")); tai.setTea_AverageScore(rs.getString("tea_AverageScore")); tai.setTea_GoodCount(rs.getString("tea_GoodCount")); tai.setTea_DefaultGoodCount(rs.getString("tea_DefaultGoodCount")); tai.setTea_GoodRate(rs.getString("tea_GoodRate")); tai.setTea_AfterCount(rs.getString("tea_AfterCount")); tai.setTea_VideoCount(rs.getString("tea_VideoCount")); tai.setTea_PoorCount(rs.getString("tea_PoorCount")); tai.setTea_GeneralCount(rs.getString("tea_GeneralCount")); tai.setTea_id(rs.getString("tea_id")); tai.setTea_detail(rs.getString("tea_detail")); tai.setTea_price(rs.getString("tea_price")); tai.setTea_source(rs.getString("tea_source")); tais.add(tai); } //往分页对象里面设置数据 pager.setDatas(tais); pager.setPageIndex(pageIndex); pager.setPageSize(pageSize); pager.setTotalPage(totalPage); pager.setTotalRecord(totalRecord); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { DBUtil.close(rs); DBUtil.close(preparedStatement); DBUtil.close(connection); } return pager; }
jsp调用Java代码,获取数据集合
request.setCharacterEncoding("utf-8"); int pageIndex = 1; int pageSize = 10; try{ pageIndex = Integer.parseInt(request.getParameter("pageIndex")); }catch(Exception e){ } String content = request.getParameter("content"); if(content == null || "".equals(content)){ content = ""; } Tea_all_infoDao taid = new Tea_all_infoDao(); //Listusers = userDao.load(content); Pager pager = taid.load(content, pageIndex, pageSize); //获得pager中保存的list集合 Listtais = pager.getDatas();
之后自己写个循环输出显示的数据,布局等
页码显示(这里我找的是div+css那种普通的分页模式,jqurey的很精美,但我不会改,暂且这样)
CSS
/* CSS 翻页代码 */ DIV.tres { PADDING-RIGHT: 7px; PADDING-LEFT: 7px; FONT-WEIGHT: bold; FONT-SIZE: 13.2pt; PADDING-BOTTOM: 7px; MARGIN: 3px; PADDING-TOP: 7px; FONT-FAMILY: Arial, Helvetica, sans-serif; TEXT-ALIGN: center } DIV.tres A { BORDER-RIGHT: #d9d300 2px solid; PADDING-RIGHT: 5px; BORDER-TOP: #d9d300 2px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #d9d300 2px solid; COLOR: #fff; PADDING-TOP: 2px; BORDER-BOTTOM: #d9d300 2px solid; BACKGROUND-COLOR: #d90; TEXT-DECORATION: none } DIV.tres A:hover { BORDER-RIGHT: #ff0 2px solid; BORDER-TOP: #ff0 2px solid; BORDER-LEFT: #ff0 2px solid; COLOR: #000; BORDER-BOTTOM: #ff0 2px solid; BACKGROUND-COLOR: #ff0 } DIV.tres A:active { BORDER-RIGHT: #ff0 2px solid; BORDER-TOP: #ff0 2px solid; BORDER-LEFT: #ff0 2px solid; COLOR: #000; BORDER-BOTTOM: #ff0 2px solid; BACKGROUND-COLOR: #ff0 } DIV.tres SPAN.current { BORDER-RIGHT: #fff 2px solid; PADDING-RIGHT: 5px; BORDER-TOP: #fff 2px solid; PADDING-LEFT: 5px; FONT-WEIGHT: bold; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #fff 2px solid; COLOR: #000; PADDING-TOP: 2px; BORDER-BOTTOM: #fff 2px solid } DIV.tres SPAN.disabled { DISPLAY: none }
可以自行修改样式