jsp資料庫查詢分頁技術

來源:互聯網
上載者:User

package bean;

public class Page {
 private int totalPage;//總頁數
 private int currentPage;//當前頁數
 private int totalRecord;//總記錄條數
 private int currentRecord;//目前記錄數
 private int pageSize=1;//每頁顯示的記錄數
 
 public int getCurrentPage() {
  return currentPage;
 }
 
 public void setCurrentPage(int PcurrentRecord,int PpageSize) {
  if(currentRecord%pageSize==0)
   currentPage=currentRecord/pageSize;
  else
   currentPage=currentRecord/pageSize+1;
 }
 
 public int getCurrentRecord() {
  return currentRecord;
 }
 
 public void setCurrentRecord(int PcurrentRecord) {
  currentRecord=PcurrentRecord;
 }
 
 public int getPageSize() {
  return pageSize;
 }
 
 public void setPageSize(int PpageSize) {
  pageSize=PpageSize;
 }
 
 public int getTotalPage() {
  return totalPage;
 }
 
 public void setTotalPage(int PtotalRecord,int PpageSize) {
  if(PtotalRecord%PpageSize==0)
   totalPage=PtotalRecord/PpageSize;
  else
   totalPage=PtotalRecord/PpageSize+1;
 }
 
 public int getTotalRecord() {
  return totalRecord;
 }
 
 public void setTotalRecord(int PtotalRecord) {
  totalRecord=PtotalRecord;
 }
}

 

 

 

<%@page import="java.util.List"%>
<%@page import="bean.Guest"%>
<%@page import="java.util.ArrayList"%>
<%@ page contentType="text/html; charset=utf-8" language="java"
 import="java.sql.*" errorPage=""%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<jsp:useBean id="db" class="bean.ORMDBUtil" scope="page"></jsp:useBean>
<jsp:useBean id="pager" class="bean.Page" scope="page"></jsp:useBean>
<%
 String sql = "select * from guests";
 int currentRecord = 0;
 ArrayList<Guest> result = db.select(sql);
 pager.setTotalRecord(result.size());
 pager.setTotalPage(result.size(), pager.getPageSize());

 if (request.getParameter("currentRecord") != null) {
  currentRecord = Integer.parseInt(request.getParameter("currentRecord"));
  pager.setCurrentRecord(currentRecord);
  pager.setCurrentPage(currentRecord, pager.getPageSize());
 }

 //取出當前頁面要顯示的子集
 List<Guest> subResult = null;
 if (currentRecord == 0);
  subResult = result.subList(0, pager.getPageSize());
 if (pager.getCurrentRecord() + pager.getPageSize() < result.size())
  subResult = result.subList(pager.getCurrentRecord(),
    pager.getCurrentRecord() + pager.getPageSize());
 else
  subResult = result.subList(pager.getCurrentRecord(),
    result.size());
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>分頁顯示JavaBean使用樣本</title>
</head>

<body>
<font size="+2"><strong>本頁利用JavaBean實現分頁顯示功能。</strong><br />
下面的表格就是運行結果 </font>
<table width="387" border="1" height="47">
 <tr>
  <td height="41">編號</td>
  <td>姓名</td>
  <td>電話</td>
  <td>email</td>
 </tr>
 <%
  if (subResult.isEmpty() == false) {
   for (int i = 0; i < subResult.size(); i++) {
    Guest guest = subResult.get(i);
    out.print("<tr>");
    out.print("<td>" + guest.getId() + "</td>");
    out.print("<td>" + guest.getCname() + "</td>");
    out.print("<td>" + guest.getPhone() + "</td>");
    out.print("<td>" + guest.getEmail() + "</td>");
    out.print("</tr>");
   }
  }
 %>
</table>
<span><font size="+2">總<%=pager.getTotalRecord()%>條記錄|總<%=pager.getTotalPage()%>頁|當前<%=pager.getCurrentPage() + 1%>頁|每頁
<%=pager.getPageSize()%>條| <%
 if (pager.getCurrentRecord() - pager.getPageSize() < 0)
  out.print("首頁|");
 else
  out.print("<a href='PageQuery.jsp?currentRecord="
    + (pager.getCurrentRecord() - pager.getPageSize())
    + "&pageSize=" + pager.getPageSize() + "'>上一頁,</a>|");

 if (pager.getCurrentRecord() + pager.getPageSize() > pager
   .getTotalRecord())
  out.print("尾頁");
 else
  out.print("<a href='PageQuery.jsp?currentRecord="
    + (pager.getCurrentRecord() + pager.getPageSize())
    + "&pageSize=" + pager.getPageSize() + "'>下一頁</a>|");
%>
</font></span>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.