我的一个详细的jsp分页程序!(oracle+jsp+apache) 一 前提 希望最新的纪录在开头给你的表建立查询: 表:mytable 查询:create or replace view as mytable_view from mytable order by id desc 其中,最好使用序列号create sequence mytable_sequence 来自动增加你的纪录id号 二 源程序 <%String sConn="你的连接" Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn=DriverManager.getConnection(sConn,"你的用户名","密码"); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); Statement stmtcount=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rs=stmt.executeQuery("select * from mytable_view"); String sqlcount="select count(*) from mytable_view"; ResultSet rscount=stmtcount.executeQuery(sqlcount); int pageSize=你的每页显示纪录数; int rowCount=0; //总的记录数 while (rscount.next()){ rowCount=rscount.getInt(1); } int pageCount; //总的页数 int currPage; //当前页数 String strPage; strPage=request.getParameter("page"); if (strPage==null){ currPage=1; } else{ currPage=Integer.parseInt(strPage); if (currPage<1) currPage=1; } pageCount=(rowCount+pageSize-1)/pageSize; if (currPage>pageCount) currPage=pageCount; int thepage=(currPage-1)*pageSize; int n=0; rs.absolute(thepage+1); while (n<(pageSize)&&!rs.isAfterLast()){%> 你要罗列的纪录 <% rs.next(); n++; } %> <%rs.close(); rscount.close(); stmt.close(); stmtcount.close(); conn.close(); %> //下面是 第几页等 <form name="sinfo" method="post" action="sbinfo_index.jsp?condition=<%=condition%>&type=<%=type%>" onSubmit="return testform(this)"> 第<%=currPage%>页 共<%=pageCount%>页 共<%=rowCount%>条 <%if(currPage>1){%><a href="sbinfo_index.jsp?condition=<%=condition%>&type=<%=type%>">首页</a><%}%> <%if(currPage>1){%><a href="sbinfo_index.jsp?page=<%=currPage-1%>&condition=<%=condition%>&type=<%=type%>">上一页</a><%}%> <%if(currPage<pageCount){%><a href="sbinfo_index.jsp?page=<%=currPage+1%>&condition=<%=condition%>&type=<%=type%>">下一页</a><%}%> <%if(pageCount>1){%><a href="sbinfo_index.jsp?page=<%=pageCount%>&condition=<%=condition%>&type=<%=type%>">尾页</a><%}%> 跳到<input type="text" name="page" size="4" style="font-size:9px">页 <input type="submit" name="submit" size="4" value="GO" style="font-size:9px"> </form> 希望大家喜欢

解决方案 »

  1.   

    基于SQL分页算法 (以前收藏的)我们知道, SQL 是一种功能性非常强的数据库编程语言, 同时它也是一个工业标准. 因此, 如果我们能够演绎出一套基于 SQL 的有效分页算法, 那么也就兼顾了算法的可移植性. 本文将要介绍的正是这样一种分页算法.  1. 假设 records_per_page 包含每页记录数 2. 查询总记录数并计算总页数  (1) 查询总记录数 total_records select count(*) from your-table-list  
    where your-criteria (2) 计算总页数 total_pages total_pages = (total_records + records_per_page - 1) 
    / records_per_page 3. 假设 current_page 包含当前页号 则其约束区间为 [1, total_pages] 4. 计算未显示记录数 not_shown_records not_shown_records = total_records -  
    (current_page - 1) * records_per_page 5. 查询当前页所有记录 select top records_per_page * 
    from ( 
    select top not_shown_records * 
    from your-table-list 
    where your-criteria 
    order by your-sort-field desc 
    ) a 
    order by a.your-sort-field asc 注意: your-sort-field 包含了一个排序字段. 你可以指定 
    多个排序字段, 但是必须保证里外包含相同的排序字段. 
    外层的每个排序字段前加上"a." 该算法在同一查询Session中, 只需要查询总记录数并计算总页数一次. 然后, 在不同页面之间传递总记录数和总页数. 每次只查询当前页所需记录. 这样不仅节省了连接资源, 而且效率也高, 同时兼顾了移植性. 你不仅可以在微软ASP中使用该分页算法, 也可以在SUN J2EE平台上轻松实现, 你甚至可以在CGI/PERL中使用该技术. 三. 实现示例 下面的程序示例使用了微软ASP技术和SQL7.0 Northwind数据库中的Orders表来演示如何实现这一分页算法.这是一个完整的经测试过的程序. 由于程序注释很详细, 这里就不再赘述. <%@ Language=JavaScript%> 
    <% 
    //------------------------------------------------------------- 
    // dbpaging.asp 
    //------------------------------------------------------------- 
    Response.Expires = -1; // 定义变量 
    var nRecordsPerPage, nTotalRecords, nTotalPages, nCurrentPage, nNotShownRecords; 
    var nFirstPage, nPrevPage, nNextPage, nLastPage; 
    var szConnection, oConnection, oRecordset, szSql; 
    var szShowPageUrl; // 查询同一查询Session不同页面之间传递参数 
    nTotalRecords = parseInt(Request.QueryString("totalrecords")); 
    nTotalPages = parseInt(Request.QueryString("totalpages")); 
    nCurrentPage = parseInt(Request.QueryString("showpage")); // 设置每页显示记录数 
    nRecordsPerPage = 10; // ODBC连接参数: 使用你自己的DSN,帐号以及密码 
    // 这里Northwind应指向微软SQL7.0 Northwind示例数据库 
    szConnection = "DSN=Northwind;UID=test;PWD=;"; 
    oConnection = Server.CreateObject("ADODB.Connection"); 
    oConnection.Open(szConnection); // 检测是否首次进入该查询Session 
    if (isNaN(nCurrentPage)) { 
    // 是, 设置第一页为当前页, 查询总记录数并计算总页数  
    nCurrentPage = 1; 
    szSql = "select count(*) from Orders"; 
    oRecordset = oConnection.Execute(szSql); 
    nTotalRecords = oRecordset(0).value; 
    oRecordset.Close(); 
    oRecordset = null; nTotalPages = parseInt((nTotalRecords + nRecordsPerPage - 1) / nRecordsPerPage); 
    } // 设置页面切换参数 
    nFirstPage = 1; // 首页 
    nPrevPage = nCurrentPage - 1; // 前一页 
    nNextPage = nCurrentPage + 1; // 下一页 
    nLastPage = nTotalPages; // 最后页 
    if (nPrevPage < 1) nPrevPage = 0; 
    if (nNextPage > nTotalPages) nNextPage = 0; // 计算未显示记录数 
    nNotShownRecords = nTotalRecords - (nCurrentPage - 1) * nRecordsPerPage;  // 查询当前页所有记录 
    szSql = "select top " + nRecordsPerPage + " * " +  
    "from ( " +  
    "select top " + nNotShownRecords + " OrderID, CustomerID, " +  
    "ShipName, ShipAddress, ShipCity, ShipPostalCode, ShipCountry " +  
    "from Orders " +  
    "order by OrderID desc " +  
    ") a " +  
    "order by a.OrderID asc"; 
    oRecordset = oConnection.Execute(szSql); 
    %> <html><head><title>DB Paging Demo</title></head><body><center> 
    <% 
    // 显示当前页所有记录 
    if (!oRecordset.EOF) { 
    %><table cellpadding="2" cellspacing="0" border="1" width="800"><tr> 
    <td align="center"><b>Order ID</b></td> 
    <td align="center"><b>Customer ID</b></td> 
    <td align="center"><b>Ship Name</b></td> 
    <td align="center"><b>Ship Address</b></td> 
    <td align="center"><b>Ship City</b></td> 
    <td align="center"><b>Ship Postal Code</b></td> 
    <td align="center"><b>Ship Country</b></td> 
    </tr> 
    <%  
    oRecordset.MoveFirst(); 
    while(!oRecordset.EOF) { 
    %><tr> 
    <td align="center"><%=oRecordset("OrderID").value%></td> 
    <td align="left"><%=oRecordset("CustomerID").value%></td> 
    <td align="left"><%=oRecordset("ShipName").value%></td> 
    <td align="left"><%=oRecordset("ShipAddress").value%></td> 
    <td align="left"><%=oRecordset("ShipCity").value%></td> 
    <td align="left"><%=oRecordset("ShipPostalCode").value%></td> 
    <td align="left"><%=oRecordset("ShipCountry").value%></td> 
    </tr>  
    <% 
    oRecordset.MoveNext(); 

    %></table><% 

    oRecordset.Close(); 
    oRecordset = null; // 设置页面切换链接参数 
    szShowPageUrl = "dbpaging.asp?totalrecords=" + nTotalRecords 
    + "&totalpages=" + nTotalPages + "&showpage="; %><table cellpadding="2" cellspacing="0" border="0" width="600"><tr> 
    <td align="center"> </td> 
    <td align="center"> <b><%  
    // 显示首页链接 
    if (nPrevPage > 0) { 
    %><p><a href="<%=szShowPageUrl%><%=nFirstPage%>">First</a><% 

    %></b></td> 
    <td align="center"> <b><%  
    // 显示前一页链接 
    if (nPrevPage > 0) { 
    %><p><a href="<%=szShowPageUrl%><%=nPrevPage%>">Previous</a><% 

    %></b></td> 
    <td align="center"> </td> 
    <td align="center"> <b><%  
    // 显示下一页链接 
    if (nNextPage > 0 ) { 
    %><p><a href="<%=szShowPageUrl%><%=nNextPage%>">Next</a><% 

    %></b></td> 
    <td align="center"> <b><%  
    // 显示最后页链接 
    if (nNextPage > 0 ) { 
    %><p><a href="<%=szShowPageUrl%><%=nLastPage%>">Last</a><% 

    %></b></td> 
    <td align="center"> </td> 
    </tr></table> 
    </center></body></html> 
      

  2.   

    不行啊!我的oracle中的这个表有一万条数据,我每次点分页,就要把这一万条数据一起查出,不行啊!
      

  3.   

    用jdbc2.0啊,可以定位到某一条记录上去,不必读出所有数据
      

  4.   

    你的问题在于数据库查询,你可以定一次可以查询多少条记录,如20条等。
    然后再进行分页处理,一次查询20条记录显示在页面上,如oracle中可以用
    select * from 
    (select a.*,rownum mynum from 表名 a where ... order by ...)
    where 
      mynum>20 and mynum<40
      

  5.   

    sybase数据库的话有没有好办法?
      

  6.   

    to weidegong ,bullcat  请问如果根据条件查询,那么原总记录数select count(*) from mytable_view"; 及select count(*) from Orders 将不会代表总记录数目了!请问如果加条件查询该如何更改呢?
      

  7.   

    不行. 我用jdbc2.0 oracle数据库.但只要我用直接定位的方法 .则在页面上显示的数据就是十六进制的,不知道为什么.  谁有解决方法?
      

  8.   

    如果根据条件查询,那么原总记录数可由如下得到:
    select count(*) from (select a.*,rownum mynum from 表名 a where ... order by ...)