Java实现分页代码
在项目中,分页是一个项目中必不可少的,它可以防止我们从数据库中进行大量数据查询时速度变慢,提高我们的查询效率。
1、定义分页模型:PageModel
package com.common.page; import java.util.List; /** * 封装分页信息 * @author Administrator * */ public class PageModel<E> { //结果集 private List<E> list; //查询记录数 private int totalRecords; //每页多少条数据 private int pageSize; //第几页 private int pageNo; /** * 总页数 * @return */ public int getTotalPages() { return (totalRecords + pageSize - 1) / pageSize; } /** * 取得首页 * @return */ public int getTopPageNo() { return 1; } /** * 上一页 * @return */ public int getPreviousPageNo() { if (pageNo <= 1) { return 1; } return pageNo - 1; } /** * 下一页 * @return */ public int getNextPageNo() { if (pageNo >= getBottomPageNo()) { return getBottomPageNo(); } return pageNo + 1; } /** * 取得尾页 * @return */ public int getBottomPageNo() { return getTotalPages(); } public List<E> getList() { return list; } public void setList(List<E> list) { this.list = list; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } }
2、分页测试:在MySQL中建立admin表,里面有字段id、name、password
3、简历Admin的实体bean类:
package com.common.page; public class Admin { private int id; private String name; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
4、测试调用:
package com.common.page; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.common.db.DbUtil; public class Client { public static PageModel findAdmins(int pageNo,int pageSize){ Connection conn=DbUtil.getConnection(); String sql="select * from admin limit ?,?"; PageModel pageModel=null; PreparedStatement pstm=null; ResultSet rs=null; Admin admin=null; List<Admin> list=new ArrayList<Admin>(); try { pstm=conn.prepareStatement(sql); pstm.setInt(1, (pageNo-1)*pageSize); pstm.setInt(2, pageNo*pageSize); rs=pstm.executeQuery();; while(rs.next()){ admin=new Admin(); admin.setId(rs.getInt("a_id")); admin.setName(rs.getString("a_name")); admin.setPassword(rs.getString("a_pwd")); list.add(admin); } ResultSet rs2=pstm.executeQuery("select count(*) from admin"); int total=0; if(rs2.next()){ total=rs2.getInt(1); } pageModel=new PageModel(); pageModel.setPageNo(pageNo); pageModel.setPageSize(pageSize); pageModel.setTotalRecords(total); pageModel.setList(list); } catch (SQLException e) { e.printStackTrace(); }finally{ DbUtil.close(conn); DbUtil.close(pstm); DbUtil.close(rs); } return pageModel; } public static void main(String[] args) { PageModel pageModel=Client.findAdmins(2,4); List<Admin> list=pageModel.getList(); for(Admin a:list){ System.out.print("ID:"+a.getId()+",用户名:"+a.getName()+",密码:"+a.getPassword()); System.out.println(); } System.out.print("当前页:"+pageModel.getPageNo()+" "); System.out.print("共"+pageModel.getTotalPages()+"页 "); System.out.print("首页:"+pageModel.getTopPageNo()+" "); System.out.print("上一页:"+pageModel.getPreviousPageNo()+" "); System.out.print("下一页:"+pageModel.getNextPageNo()+" "); System.out.print("尾页:"+pageModel.getBottomPageNo()+" "); System.out.print("共"+pageModel.getTotalRecords()+"条记录"); System.out.println(); } }
这样分页效果就实现了,我们要实现分页效果,只要传入相应的参数和相应的数据库执行语句即可实现,希望大家能灵活运用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)