jsp 分頁(資料庫讀取資料)

來源:互聯網
上載者:User
<%@ page contentType="text/html; charset=gb2312"%><%@ page language="java"%><%@ page import="java.sql.*"%><%//驅動程式名,比較舊了,如果你用mysql5,自己改。String driverName = "org.gjt.mm.mysql.Driver";String userName = "root";//資料庫使用者名稱String userPasswd = "666666";//密碼String dbName = "search";//資料庫名String tableName = "organizations"; //表名//連接字串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 = 30;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"></head><%//擷取總記錄數ResultSet rs = statement.executeQuery("select count(*) from " + tableName);rs.next();RecordCount = rs.getInt(1);rs = statement.executeQuery("SELECT serial,name,domain FROM " + tableName + " ORDER BY serial LIMIT " + StartRow + ", " + PageSize);//擷取總頁數MaxPage = RecordCount % PageSize;if (RecordCount % PageSize == 0) {MaxPage = RecordCount / PageSize;} else {MaxPage = RecordCount / PageSize + 1;}%><body class="UsePageBg"><table width="100%" border="1" class="InternalHeader"><tr><td width="24%"><font size=4>分頁顯示記錄</font></td><td width="76%"><font size=4><%="總共" + RecordCount + "條記錄 - 當前頁:" + PageNo + "/"+ MaxPage%></font></td></tr></table><br><table width="100%" border="1" class="NormalTableTwo"><tr><td class="InternalHeader">序號</td><td class="InternalHeader">開辦單位</td><td class="InternalHeader">登入地址</td></tr><%int i = 1;while (rs.next()) {int bil = i + (PageNo - 1) * PageSize;%><tr><td class="NormalFieldTwo"><%=bil%></td><td class="NormalFieldTwo"><%=rs.getString(2)%></td><td class="NormalFieldTwo"><%=rs.getString(3)%></td></tr><%i++;}%></table>><br><table width="100%" border="0" class="InternalHeader"><tr><td><div align="center"><%out.print("<font size=4>");//顯示第一頁或者前一頁的連結//如果當前頁不是第1頁,則顯示第一頁和前一頁的連結if (PageNo != 1) {PrevStart = PageNo - 1;out.print("<a href=search.jsp?PageNo=1>第一頁 </a>: ");out.print("<a href=search.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=search.jsp?PageNo=" + c + ">" + c + "</a>");} else {out.print("<a href=search.jsp?PageNo=" + c + ">" + c + "</a> ,");}} else {if (PageNo == MaxPage) {out.print(c);break;} else {out.print("<a href=search.jsp?PageNo=" + c + ">" + c + "</a>");break;}}}out.print("]");;if (PageNo < MaxPage) { //如果當前頁不是最後一頁,則顯示下一頁連結NextPage = PageNo + 1;out.print("<a href=search.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=search.jsp?PageNo=" + MaxPage + ">最後一頁</a>");}out.print("</font>");%></div></td></tr></table><%rs.close();statement.close();connection.close();%></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.