jsp+MySQL的查詢結果分頁顯示[執行個體]

來源:互聯網
上載者:User

已驗證可以執行的代碼。

初學,謹作為個人學習記錄。

請大家給出意見或建議。謝謝!

 

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
 //驅動程式名,比較舊了,如果你用mysql5,自己改。
 String driverName="com.mysql.jdbc.Driver";
 String userName="root";//資料庫使用者名稱
 String userPasswd="12345";//密碼

 String dbName="ziyuan";//資料庫名

 String tableName="zyd_user"; //表名

//連接字串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName(driverName).newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();

//每頁顯示記錄數
int PageSize = 4; //每頁顯示記錄數
int StartRow = 0; //開始顯示記錄的編號
int PageNo=0;//需要顯示的頁數
int CounterStart=0;//每頁頁碼的初始值
int CounterEnd=0;//顯示頁碼的最大值
int RecordCount=0;//總記錄數;
int MaxPage=0;//總頁數
int PrevStart=0;//前一頁
int NextPage=0;//下一頁
int LastRec=0;
int LastStartRecord=0;//最後一頁開始顯示記錄的編號

//擷取需要顯示的頁數,由使用者提交
if(request.getParameter("PageNo")==null){ //如果為空白,則表示第1頁
  if(StartRow == 0){
     PageNo = StartRow + 1; //設定為1
  }
}else{
  PageNo = Integer.parseInt(request.getParameter("PageNo")); //獲得使用者提交的頁數
  StartRow = (PageNo - 1) * PageSize; //獲得開始顯示的記錄編號
}

//因為顯示頁碼的數量是動態變化的,假如總共有一百頁,則不可能同時顯示100個連結。而是根據當前的頁數顯示
//一定數量的頁面連結

//設定顯示頁碼的初始值!!
  if(PageNo % PageSize == 0){
   CounterStart = PageNo - (PageSize - 1);
  }else{
   CounterStart = PageNo - (PageNo % PageSize) + 1;
  }

CounterEnd = CounterStart + (PageSize - 1);
%>

<html>
<head>
<title>分頁顯示記錄</title>
<link rel="stylesheet" href="style.css" type="text/css">
<style type="text/css">
<!--
.STYLE13 {font-family: "幼圓"}
.STYLE18 {font-size: 14px}
-->
</style>
</head>
<%

//擷取總記錄數
ResultSet rs = statement.executeQuery("select count(*) from zyd_user" );
rs.next();
RecordCount = rs.getInt(1);

//rs = statement.executeQuery("SELECT usercode,username,password,comcode,flag_level,flag_status FROM zyd_user ORDER BY usercode DESC LIMIT "+StartRow+", "+PageSize);
rs = statement.executeQuery("SELECT usercode,username,password FROM zyd_user ORDER BY usercode LIMIT "+StartRow+", "+PageSize);

//擷取總頁數
MaxPage = RecordCount % PageSize;
if(RecordCount % PageSize == 0){
  MaxPage = RecordCount / PageSize;
}else{
   MaxPage = RecordCount/PageSize+1;
}
%>
<body class="UsePageBg STYLE13 STYLE18">
<table width="100%" border="0" bordercolor="#000000" class="InternalHeader">
 <tr>
   <td width="24%"><span class="STYLE13">分頁顯示記錄</span></td>
    <td width="76%">
    <span class="STYLE13"><%="總共"+RecordCount+"條記錄 - 當前頁:"+PageNo+"/"+MaxPage %>    </span></td>
 </tr>
</table>

<br>
<table width="100%" border="0" bordercolor="#000000" class="NormalTableTwo">
  <tr>
    <td width="18%" align="center" valign="middle" bordercolor="#000000" class="InternalHeader STYLE13 " >代碼</td>
    <td width="20%" align="center" valign="middle" bordercolor="#000000" class="InternalHeader STYLE13 " >姓名</td>
 </tr>

<%
int i = 1;
while (rs.next()) {
  int bil = i + (PageNo-1)*PageSize;
%>
 <tr>
    <td align="center" valign="middle" bordercolor="#000000" class="NormalFieldTwo" ><span class="STYLE13"><%=rs.getString(1)%></span></td>
    <td align="center" valign="middle" bordercolor="#000000" class="NormalFieldTwo" ><span class="STYLE13"><%=rs.getString(2)%></span></td>
  </tr>
<%
  i++;
}%>
</table>
<br>
<table width="100%" border="0" class="InternalHeader">
  <tr>
   <td class="STYLE13"><div align="center">
<%
   out.print("<font size=4>");
  //顯示第一頁或者前一頁的連結
  //如果當前頁不是第1頁,則顯示第一頁和前一頁的連結
  if(PageNo != 1){
    PrevStart = PageNo - 1;
    out.print("<a href=main_data.jsp?PageNo=1>第一頁 </a>: ");
    out.print("<a href=main_data.jsp?PageNo="+PrevStart+">前一頁</a>");
  }
  out.print("[");

   //列印需要顯示的頁碼
   for(int c=CounterStart;c<=CounterEnd;c++){
   if(c <MaxPage){
     if(c == PageNo){
       if(c %PageSize == 0){
         out.print(c);
       }else{
          out.print(c+" ,");
       }
     }else if(c % PageSize == 0){
        out.print("<a href=main_data.jsp?PageNo="+c+">"+c+"</a>");
     }else{
        out.print("<a href=main_data.jsp?PageNo="+c+">"+c+"</a> ,");
     }
   }else{
     if(PageNo == MaxPage){
      out.print(c);
      break;
     }else{
        out.print("<a href=main_data.jsp?PageNo="+c+">"+c+"</a>");
     break;
   }
  }
}

out.print("]");;

if(PageNo < MaxPage){ //如果當前頁不是最後一頁,則顯示下一頁連結
    NextPage = PageNo + 1;
    out.print("<a href=main_data.jsp?PageNo="+NextPage+">下一頁</a>");
}

//同時如果當前頁不是最後一頁,要顯示最後一頁的連結
if(PageNo < MaxPage){
   LastRec = RecordCount % PageSize;
   if(LastRec == 0){
      LastStartRecord = RecordCount - PageSize;
   }
   else{
      LastStartRecord = RecordCount - LastRec;
   }

   out.print(":");
    out.print("<a href=main_data.jsp?PageNo="+MaxPage+">最後一頁</a>");

  }
  out.print("</font>");
%>
</div></td>
</tr>
</table>
<span class="STYLE13">
<%
  rs.close();
  statement.close();
   connection.close();
%>
</span>
</body>
</html>

</style>

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.