jsp页面数据分页模仿百度分页效果(实例讲解)

废话不多说,直接上代码

请根据自己的项目、包名修改

<%@page import="web09.shop.DBUtil"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" pageEncoding="UTF-8"%>



  
  数据分页
  




  <%
  DBUtil dbutil=new DBUtil();
  Connection conn=dbutil.getCon();
  //Connection conn = new DBUtil().getCon();
  PreparedStatement pstmt1 = conn.prepareStatement("select count(*) from student");
  ResultSet rs1 = pstmt1.executeQuery();
  rs1.next();
  int recordCount = rs1.getInt(1);   //记录总数
  int pageSize = 10;          //每页记录数
  int start=1;            //显示开始页
  int end=10;              //显示结束页
  int pageCount = recordCount%pageSize==0 ? recordCount/pageSize : recordCount/pageSize+1; 
  int currPage = request.getParameter("p")==null ? 1 : Integer.parseInt(request.getParameter("p"));
  
  currPage = currPage<1 ? 1 : currPage;
  currPage = currPage>pageCount ? pageCount : currPage;
  
  PreparedStatement pst = conn.prepareStatement("select * from student limit ?,?");
  pst.setInt(1,currPage*pageSize-pageSize);
  pst.setInt(2,pageSize);
  ResultSet rs = pst.executeQuery();
  
  while(rs.next()){
  %>
  
  <%
  }
  %>
  
ID 姓名 年龄 专业
<%=rs.getInt(1) %> <%=rs.getString(2) %> <%=rs.getInt("age") %> <%=rs.getString(4) %>
<% out.print(String.format("首页",1)); if(currPage>=7){ start=currPage-5; end=currPage+4; } if(start>(pageCount-10)){ start=pageCount-9; } if(currPage>1){ out.print(String.format("上一页",currPage-1)); } for(int i=start;i<=end;i++){ if(i>pageCount) break; String pageinfo=String.format("%d",i,i); if(i==currPage){ pageinfo=String.format("%d",i); } out.print(pageinfo); } if(currPage<=pageCount){ out.print(String.format("下一页",currPage+1)); } out.print(String.format("尾页",pageCount)); %>

以上这篇jsp页面数据分页模仿百度分页效果(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(jsp页面数据分页模仿百度分页效果(实例讲解))