jsp+servlet+javabean实现数据分页方法完整实例(2)

package bean; import java.sql.ResultSet; public class PageProperties { private int currentPage;//当前页号 private int totalPages;//总页数 private int totalRecords;//总数据条数 private ResultSet rs;//动态结果集 public PageProperties() { super(); } public PageProperties(int currentPage, int totalPages, int totalRecords, ResultSet rs) { super(); this.currentPage = currentPage; this.totalPages = totalPages; this.totalRecords = totalRecords; this.rs = rs; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getTotalPages() { return totalPages; } public void setTotalPages(int totalPages) { this.totalPages = totalPages; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } public ResultSet getRs() { return rs; } public void setRs(ResultSet rs) { this.rs = rs; } }

5、TablePage.java

package bean; import java.sql.ResultSet; public class PageProperties { private int currentPage;//当前页号 private int totalPages;//总页数 private int totalRecords;//总数据条数 private ResultSet rs;//动态结果集 public PageProperties() { super(); } public PageProperties(int currentPage, int totalPages, int totalRecords, ResultSet rs) { super(); this.currentPage = currentPage; this.totalPages = totalPages; this.totalRecords = totalRecords; this.rs = rs; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getTotalPages() { return totalPages; } public void setTotalPages(int totalPages) { this.totalPages = totalPages; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } public ResultSet getRs() { return rs; } public void setRs(ResultSet rs) { this.rs = rs; } }

6、Page.java这是主要处理类,Servlet

package servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import util.PostgreSQL_Util; import bean.PageProperties; import bean.TablePage; public class Page extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); /** * 通过TablePage设置分页属性 * */ TablePage tb=new TablePage(); //获取当前表格显示的页码 int currentPage=tb.currentPage(tb.getStrPage(request, "page")); System.out.println(currentPage); //设置每页显示数据条数 tb.setPageRecord(10);//设置每页显示10条数据 /** * 通过xxSQL_Util设置JDBC连接及数据处理 */ PostgreSQL_Util postgres=new PostgreSQL_Util(); try { ResultSet rs_count=postgres.executeQuery("select count(*) as c from student"); rs_count.next(); //获得总的数据条数 int totalRecords=rs_count.getInt("c"); //根据数据表的总数据条数获取页面显示表格的总页数 int totalPages=tb.getTotalPages(totalRecords); if(currentPage>totalPages){ currentPage=totalPages;//保证最后一页不超出范围 } //根据数据库表信息和当前页面信息获得动态结果集 ResultSet rs=tb.getPageResultSet(postgres.executeQuery("select * from student"), currentPage); /** * 将数据加入javaBean */ PageProperties pp=new PageProperties(currentPage, totalPages, totalRecords, rs); /** * 将javaBean转发至前端 */ request.setAttribute("result", pp); request.getRequestDispatcher("tablePage.jsp").forward(request, response); } catch (SQLException e) { System.out.println("Class Page:"+e.getMessage()); // e.printStackTrace(); } } }

7、tablePage.jsp前台显示效果

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzfywj.html