| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 685 人关注过本帖, 2 人收藏
标题:【原创】jsp 实现分页功能
只看楼主 加入收藏
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
结帖率:100%
收藏(2)
 问题点数:0 回复次数:3 
【原创】jsp 实现分页功能
程序代码:
<%@ page contentType="text/html" pageEncoding="utf-8" language="java"%>  
<%@ page import="java.sql.*"%>  
<html>  
    <head>  
        <title>hello</title>  
    </head>  
    <body>  
    <table border="1" bgcolor="#FF051">  
    <thead>
        <th>id</th>
        <th>姓名</th>
        <th>性别</th>
        <th>年龄</th>
        <th>电话</th>
        <th>地址</th>
    </thead>
    <tbody>
<%!  
    public static final String driver= "com.mysql.jdbc.Driver";  
    public static final String user= "[Username]";  
    public static final String password= "[Password]";  
    public static final String url= "jdbc:mysql://localhost:3306/[DataBaseName]";  
    public static final int PAGESIZE = 3;  //控制分页数
    int pageCount;                        //控制每页显示的数据数量
     int curPage = 1;                     //当前页面的页数
%>  
<%  
    //一页放5个    
    try{  
        Class.forName(driver);  
        Connection con = DriverManager.getConnection(url,user,password);  
        String sql = "select * from [tableName]";  
        PreparedStatement stat = con.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);  
        ResultSet rs = stat.executeQuery();  
        rs.last();  
        int size = rs.getRow();  
        pageCount = (size%PAGESIZE==0)?(size/PAGESIZE):(size/PAGESIZE+1);  
        String temp = request.getParameter("curPage");  
        if(temp==null){  
            temp="1";  
        }  
        curPage = Integer.parseInt(temp);  
        if(curPage>=pageCount) curPage = pageCount; 
        if(curPage<=0){
        curPage=1;
        out.print("已经是第一页了");
        } 
       boolean flag = rs.absolute((curPage-1)*PAGESIZE+1);  
        int count = 0;  
          
        do{  
            if(count>=PAGESIZE)break;  
            String uno = rs.getString(1);  
            String username = rs.getString(2);  
            String gender = rs.getString(3);  
            int age = rs.getInt(4);  
            String phone = rs.getString(5);  
            String address = rs.getString(6);  
            count++; 
   %>  
   
     <tr>  
            <td><%=uno%></td>  
            <td><%=username%></td>  
            <td><%=gender%></td>  
            <td><%=age%></td>  
            <td><%=phone%></td>  
            <td><%=address%></td>  
        </tr>  
            <%  
        }while(rs.next()); 
        con.close();   
    }  
    catch(Exception e){  
          
    }  
%>  
</tbody>
   </table>
   <a href = "index.jsp?curPage=1" >首页</a>  
<a href = "index.jsp?curPage=<%=curPage-1%>" >上一页</a>  
<a href = "index.jsp?curPage=<%=curPage+1%>" >下一页</a>  
<a href = "index.jsp?curPage=<%=pageCount%>" >尾页</a>  
第<%=curPage%>页/共<%=pageCount%>页 
  
</body>  
</html>
搜索更多相关主题的帖子: border hello title java 电话 
2014-11-09 22:07
小小bao
Rank: 2
等 级:论坛游民
威 望:1
帖 子:13
专家分:68
注 册:2014-11-20
收藏
得分:0 
程序代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%
com.shengdaapp.util.PageResult pageResult = (com.shengdaapp.util.PageResult)request.getAttribute("page");
if(pageResult!=null){
    int totalCount =  pageResult.getPageTotal();//共多少条数据
    int pageSize = pageResult.getPageSize();//每页多少条
    int pageMax = pageResult.getPageTotalNo();//共多少页
    int totalPage = pageMax;
    int pageNo = pageResult.getPageNo();//当前的页数
    int show = 5;
    int index = 1;
    int sum = totalPage;
    if(sum > 5) sum = 5;
    if (pageNo < show) {
        index = 1;
    } else if (pageNo > totalPage - 4) {
        sum = totalPage;
        index = totalPage - 4;
    } else {
        index = pageNo - 2;
        sum = pageNo + 2;
    }
%>
<div style="text-align: right;" id="pagination">
    <input id="pageNo" value="<%=pageNo%>" type="hidden">
    <input id="pageMax" value="<%=pageMax%>" type="hidden">
  
    <% if(pageNo==1&& pageMax!=0){%>
            <font color="gray">首页</font>
       <% }else if(pageNo>=1&& pageMax!=0){%>
        <a href="javascript:void(0);" onclick="pageClick('1');" title="首页" >    首页</a>
    <% }%>
    <% if(pageNo==1 && pageMax!=0){%>  
            <font color="gray">上一页</font>
       <% }else if(pageNo>=1&& pageMax!=0){%>
        <a href="javascript:void(0);" onclick="pageClick('<%=pageNo <= 1 ? "1" : pageNo-1%>');" title="上一页">上一页</a>
       <% }%>
    <% for(int i =index;i<=sum ;i++)  {%>
     
        <a href="javascript:void(0);" onclick="pageClick('<%=i%>');"  <%if(i == pageNo) { out.println("style=\"color:#FF0000\"");}%>><%=i%></a>
    <%} %>
    <% if(pageNo==pageMax && pageMax!=0){%>
           <font color="gray">下一页</font>
     <% }else if(pageNo!=pageMax && pageMax!=0){%>
         <a href="javascript:void(0);" onclick="pageClick('<%=pageNo == totalPage ? totalPage : pageNo+1%>');" title="下一页">下一页</a>
    <% }%>

    <% if(pageNo==pageMax && pageMax!=0){%>
           <font color="gray">尾页</font>
    <% }else if(pageNo!=pageMax && pageMax!=0){%>
          <a href="javascript:void(0);" onclick="pageClick('<%=totalPage%>');"  title="尾页">尾页</a>
     <% }%>
     <%  if(pageMax!=0){%>
    <span>
        <input id="goVal" style="width: 30;" onkeyup="this.value=this.value.replace(/[^\d]/g,'');if(this.value><%=totalPage%>)this.value=<%=totalPage%>;if(this.value<=0)this.value=1">
        <a href="javascript:void(0);" onclick="goClick();">GO</a></span>
    <span>共<%=totalPage%>页,<%=pageResult.getPageTotal()==-1?0:pageResult.getPageTotal()%>条记录</span>
    <% }%>
&nbsp;&nbsp;</div>
<script type="text/javascript">
<!--
function goClick(){
    var goval = $("#goVal").val();
    if(goval><%=pageMax%>){
        goval= <%=pageMax%>;
    }

    pageClick(goval);
}
function pageClick(pageNo){
    var pageInput = ("<input type='hidden' value='"+pageNo+"' name='pageNo'  id='formPageNo'/>");
    $("#queryform").append(pageInput);
    $("#queryform").submit();
}
//-->
</script>
<%}%>
我这也有个分页,楼主给看看
2014-11-20 11:25
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
收藏
得分:0 
javascript?

思考赐予新生,时间在于定义
2014-11-20 12:10
小小bao
Rank: 2
等 级:论坛游民
威 望:1
帖 子:13
专家分:68
注 册:2014-11-20
收藏
得分:0 
恩恩,怎么样,可有改进的地方
2014-11-20 13:36
快速回复:【原创】jsp 实现分页功能
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018112 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved