java中的分页(最基础的方法)
2015-09-04 12:18
495 查看
/** * 有关该方法的简介 * PageModel 类,分页的实体类 * UtilDB 类,数据库的关闭与连接 * StudentDao 类,执行分页查询的类 * Fenye02-1 类,servlet处理类 * fenye02-1.jsp ,显示结果的页面 * * 注:该分页方法连接的数据库是Oracle数据库,如果要连接sqlserver数据库,则改变连接的方式即可,其他的都一样 * */ //PageModel 类 import java.util.List; public class PageModel<T> { // 定义当前页 private int pageNo; // 每页显示的记录数 private int pageSize; // 总记录数 private int count; // 数据集合 private List<T> all; public List<T> getAll() { return all; } public void setAll(List<T> all) { this.all = all; } /** * 首页 * * @return */ public int getIndex() { return 1; } /** * 尾页 * * @return */ public int getLastPage() { if (this.getCount() % this.getPageSize() == 0) { return this.getCount() / this.getPageSize(); } else { return this.getCount() / this.getPageSize() + 1; } } /** * 上一页 * * @return */ public int getPagePerv() { if (this.getPageNo() - 1 < 1) { return 1; } else { return this.getPageNo() - 1; } } /** * 下一页 * * @return */ public int getPageNext() { if (this.getPageNo() + 1 > this.getLastPage()) { return this.getLastPage(); } else { return this.getPageNo() + 1; } } /** * 各属性的 get/set 方法 * * @return */ public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } } //UtilDB 类 import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class UtilDB { /** * 得到数据库操作对象 */ private static Connection con=null; /** * 链接数据库 * @return */ public static Connection getCon(){ try { Class.forName("oracle.jdbc.driver.OracleDriver"); con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger"); } catch (Exception e) { e.printStackTrace(); } return con; } /** * 关闭数据库 * @param pst * @param rs * @param con * @throws SQLException */ public static void closeDB(PreparedStatement pst,ResultSet rs,Connection con) throws SQLException{ if(pst!=null){ pst.close(); } if(rs!=null){ rs.close(); } if(con!=null){ con.close(); } } } //StudentDao 类 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 util.UtilDB; import entity.PageModel; import entity.StudentInfo; public class StudentDao { /** * 得到数据库操作对象 */ private static PreparedStatement pst=null; private static ResultSet rs=null; private static Connection con=null; public PageModel<StudentInfo> selectAll(int pageNo,int pageSize){ //创建 pagemodel 对象 PageModel<StudentInfo> pm=new PageModel<StudentInfo>(); //创建集合,用于封装 student 信息 List<StudentInfo> slist=new ArrayList<StudentInfo>(); try { //链接数据库 con=UtilDB.getCon(); pst=con.prepareStatement("select * from (select rownum r ,s.* from studentinfo s) where r between ("+pageNo+"-1)*10+1 and "+pageNo+"*"+pageSize+""); //执行 sql 语句 rs=pst.executeQuery(); while(rs.next()){ //下标从 2 开始,因为 下标为 1 的是 rownum,如果从1开始会报错 StudentInfo s=new StudentInfo(rs.getInt(2), rs.getString(3), rs.getString(4), rs.getInt(5)); slist.add(s); } //为 pagemodel 类设置值 pm.setAll(slist); pm.setPageNo(pageNo); pm.setPageSize(pageSize); pm.setCount(this.count()); } catch (Exception e) { e.printStackTrace(); }finally{ try { //关闭数据库 UtilDB.closeDB(pst, rs, con); } catch (Exception e) { e.printStackTrace(); } } return pm; } /** * 得到总共有多少条数据 * @return */ public int count(){ int count=0; try { //链接数据库 con=UtilDB.getCon(); pst=con.prepareStatement("select count(*) from studentinfo"); //执行sql rs=pst.executeQuery(); if(rs.next()){ count=rs.getInt(1); } } catch (Exception e) { e.printStackTrace(); }finally{ try { //关闭数据库 UtilDB.closeDB(pst, rs, con); } catch (SQLException e) { e.printStackTrace(); } } return count; } } //通过一个超链接跳转到该servlet处理类来 //Fenye02-1 servlet类 import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import dao.StudentDao; import entity.PageModel; import entity.StudentInfo; public class Fenye02_1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //最好调用doPost方法 doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取页面传过来的当前页的页码(pageNo) String str=request.getParameter("pageNo"); StudentDao dao=new StudentDao(); //当前页 int pageNo=1; //每页显示的条数 int pageSize=10; //第一次到该servlet时,pageNo为null,所以比较pageNo是否为null,如果不为null,则改变当前页的页码 if(str!=null&&!"".equals(str)){ pageNo=Integer.parseInt(str); } //调用分页查询的方法 PageModel<StudentInfo> pm=dao.selectAll(pageNo, pageSize); //将查询的结果保存到请求中 request.setAttribute("pm", pm); //跳转 request.getRequestDispatcher("../fenye02_1.jsp").forward(request, response); } } //fenye02-1.jsp 页面的主要代码 <% PageModel<StudentInfo> pm=(PageModel<StudentInfo>)request.getAttribute("pm"); %> </head> <body> <hr/> <table border="1" cellpadding="0" cellspacing="0" align="center" width="60%" height=""> <tr align="center"> <td>学号</td> <td>姓名</td> <td>性别</td> <td>年龄</td> </tr> <% for(StudentInfo s:pm.getAll()){ %> <tr align="center"> <td><%=s.getStuNo() %></td> <td><%=s.getName() %></td> <td><%=s.getSex() %></td> <td><%=s.getAge() %></td> </tr> <% } %> <tr> <td colspan="4" align="center"> <a href="servlet/Fenye02_1?pageNo=1">首页</a> <a href="servlet/Fenye02_1?pageNo=<%=pm.getPagePerv() %>">上一页</a> <a href="servlet/Fenye02_1?pageNo=<%=pm.getPageNext() %>">下一页</a> <a href="servlet/Fenye02_1?pageNo=${pm.lastPage}">尾页</a> </td> </tr> </table> </body>
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树