首页 > 编程 > JSP > 正文

高效简单的jsp分頁

2024-09-05 00:20:25
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • <%@ page import="java.sql.*,java.io.*,java.util.*" %>
    <%@ page language="java" pageencoding="big5" %>
    <%! string au_id,au_lname,au_fname,phone,address,city,state;%>
    <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
    <html>
    <head>
    <title>articlelist</title>
    <link rel="stylesheet" href="style.css">
    </head>
    <body bgcolor="#ffffff">
    <table width="760" border="0" cellspacing="0" cellpadding="0" align="center">
    <tr>
    <td>
    <table width="750" border="0" cellspacing="2" cellpadding="2" align="center">
    <tr align="center">
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>
    <table width="740" border="0" cellspacing="1" cellpadding="1" align="center">
    <tr align="center" bgcolor="#cccccc">
    <td><b>標題</b></td>
    <td><b>作者</b></td>
    <td><b>類型</b></td>
    <td><b>點擊</b></td>
    <td><b>加入時間</b></td>
    </tr>
    <%
    int i;
    int ipage=5; //分頁單位
    int allpage; //總頁數
    int pages; //接受的頁碼變數
    int cpage=1; //當前頁
    int spage; //開始頁
    int allcol=0;
    vector v=null;
    //資料庫連接聲明
    connection conn=null;
     java.lang.string sql,pagesql;
     java.sql.resultset rs,pagers;
     statement statement=null;
     string kmsdb="jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=dbname";
     class.forname("com.microsoft.jdbc.sqlserver.sqlserverdriver");
        conn = drivermanager.getconnection(kmsdb, "user", "pwd");
     
     pagesql = "select count(*) from  authors";
    //取總文章數
    statement = conn.createstatement();
    pagers = statement.executequery(pagesql);
     while(pagers.next())
      {
      allcol = pagers.getint(1);
      system.out.println(allcol);
      }
     
    //獲得總頁面數
    allpage = (int)math.ceil((allcol + ipage-1) / ipage);
    //判斷參數pages是否空
    if (request.getparameter("pages")==null) {
    pages=1;
    } else {
    pages = new integer(request.getparameter("pages")).intvalue();
    system.out.println(pages);
    }
    //判斷當前頁
    if(pages > allpage || pages == 0)
    {cpage = 1;}
    else
    {
    cpage = pages;
    }
    //判斷起始頁
    sql = "select top "+ipage+" au_id,au_lname,au_fname,address,phone from authors where au_id not in (select top "+(cpage-1)*ipage+" au_id from authors order by au_id desc)order by au_id desc";
    rs = statement.executequery(sql);
    while (rs.next())
    {
     au_id=rs.getstring("au_id");
     au_lname=rs.getstring("au_lname");
     au_fname=rs.getstring("au_fname");
     phone=rs.getstring("phone");
     address=rs.getstring("address");
    %> 
     <td width="300"><%= au_id %></a></td>
     <td width="300"><%= au_lname %></a></td>
     <td width="300"><%= au_fname %></a></td>
     <td width="300"><%= phone %></a></td>
     <td width="300"><%= address %></a></td>
    </tr>
    <%    
    }%>
    <tr bgcolor="#cccccc">
    <td colspan="5" align="right" valign="middle">共 <%= allcol %>條記錄 共 <%= allpage %>頁 <% if (cpage>1) { %><a href="fy.jsp?pages=<%= cpage-1 %>"><img src="http://www.VeVb.com/htmldata/2005-03-09/images/prev.gif" border="0"></a><% } if (cpage<allpage) { %><a href="fy.jsp?pages=<%= cpage+1 %>"><img src="http://www.VeVb.com/htmldata/2005-03-09/images/next.gif" border="0"></a><% } %> / <% for (i=1;i<=allpage;i++) { if (i != pages) {%><a href="fy.jsp?pages=<%= i %>"><%= i %></a>&nbsp;<% }} %></td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    </body>
    </html>
    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表