欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-17 21:09:46
废话不多说,直接上代码 请根据自己的项目、包名修改 <%@page import="web09.shop.dbutil"%> <%@page...

废话不多说,直接上代码

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

<%@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"%>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>数据分页</title>
  <style type="text/css">
    .page a{
      min-width: 34px;
      height: 34px;
      border: 1px solid #e1e2e3;
      cursor: pointer;
      display:block;
      float: left;
      text-decoration: none;
      text-align:center;
      line-height: 34px;      
    }
    
    .page a:hover {
      background: #f2f8ff;
      border: 1px solid #38f ;
    }
    .page a.prev{
      width:50px;
    }
    .page span{
      width: 34px;
      height: 34px;
      border: 1px solid transparent;
      cursor: pointer;
      display:block;
      float: left;
      text-decoration: none;
      text-align:center;
      line-height: 34px;
      cursor: default;
    }
  </style>
</head>

<body>
<table class="tt" border="1" align="center" width="80%" cellpadding="10">
  <tr>
    <th>id</th>
    <th>姓名</th>
    <th>年龄</th>
    <th>专业</th>
  </tr>
  <%
  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()){
  %>
  <tr align="center">
  <td><%=rs.getint(1) %></td>
  <td><%=rs.getstring(2) %></td>
  <td><%=rs.getint("age") %></td>
  <td><%=rs.getstring(4) %></td>
  </tr>
  <%
  }
  %>
  <tr>
     <th colspan="4" class="page">
       <% 
         out.print(string.format("<a class=\"prev\" href=\"?p=%d\">首页</a>",1));
         if(currpage>=7){
           start=currpage-5;
           end=currpage+4;
         }
         if(start>(pagecount-10)){
           start=pagecount-9;
         }
         if(currpage>1){
           out.print(string.format("<a class=\"prev\" href=\"?p=%d\">上一页</a>",currpage-1));
         }
         
         for(int i=start;i<=end;i++){
           if(i>pagecount) break;
           string pageinfo=string.format("<a href=\"?p=%d\">%d</a>",i,i);
           if(i==currpage){
             pageinfo=string.format("<span>%d</span>",i);
           }
           out.print(pageinfo);
         }
         
         if(currpage<=pagecount){
           out.print(string.format("<a class=\"prev\" href=\"?p=%d\">下一页</a>",currpage+1));
         }
         
         out.print(string.format("<a class=\"prev\" href=\"?p=%d\">尾页</a>",pagecount)); 
       %>
     </th>
   </tr>
</table>
</body>
</html>

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