基於Mysql的分頁顯示

來源:互聯網
上載者:User

代碼寫在了下面,可以將Java代碼進行封裝,然後直接頁面調用就行了,這樣直接寫在jsp頁面中顯得比較混亂,不過寫下來防止忘記,方便查閱!!

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><%@ page import="java.sql.*;"%><%    String path = request.getContextPath();    String basePath = request.getScheme() + "://"           + request.getServerName() + ":" + request.getServerPort()           + path + "/";%> <%!boolean flag = false;    String str = "";%> <%    String login = (String) session.getAttribute("login");    if (login != null && login.equals("true")) {       flag = true;    }%> <%    int pageSize = 3; //每頁記錄的條數    int pageNo = 0; //第幾頁    int tolPage = 0; //共幾頁    Class.forName("com.mysql.jdbc.Driver");    String url = "jdbc:mysql://localhost/bbs?user=root&password=wangkang";    Connection conn = DriverManager.getConnection(url);     Statement stmt = conn.createStatement();    ResultSet rs = null;     //關於分頁的演算法--計算總頁數    Statement stmt1 = conn.createStatement();    rs = null;    rs = stmt1           .executeQuery("select count(*) from article where pid = 0");    rs.next();    int tolNum = rs.getInt(1);    stmt1.close();         //計算總頁數    if (tolNum % pageSize == 0) {       tolPage = tolNum / pageSize;    } else {       tolPage = tolNum / pageSize + 1;    }     /**     *分頁演算法     */     String pageStr = request.getParameter("pageNo");     if (pageStr == null || pageStr.equals("")) {       pageNo = 1;    } else {       if (Integer.parseInt(pageStr) <= 0) {           pageNo = 1;       } else if (Integer.parseInt(pageStr) >= tolPage) {           pageNo = tolPage;       } else {           pageNo = Integer.parseInt(pageStr);       }    }     int pagePos = (pageNo - 1) * pageSize; //起始記錄位置     //Mysql中關於分頁的sql語句    rs = stmt           .executeQuery("select * from article where pid=0 order by pdate desc limit "                  + pagePos + "," + pageSize);    String strLogin = "";    String fStr = "";     while (rs.next()) {       if (flag) {           fStr = "<a href='Delete.jsp?pid=" + rs.getInt("pid")                  + "&id=" + rs.getInt("id") + "'>刪除</a>";       }        str += "<tr><td>" + rs.getInt("id") + "</td><td>"              + "<a href='ShowDetail.jsp?id=" + rs.getInt("id")              + "'>" + rs.getString("title") + "</a>" + "</td><td>"              + fStr + "</td></tr>";    }     /*    //關於分頁的演算法--計算總頁數    Statement stmt1 = conn.createStatement();    rs = null;    rs = stmt1           .executeQuery("select count(*) from article where pid = 0");    rs.next();    int tolNum = rs.getInt(1);    stmt1.close();     if (tolNum % 3 == 0) {       tolPage = tolNum / 3;    } else {       tolPage = tolNum / 3 + 1;    }     */     /*    if (pageStr == null || pageStr.equals("")) {       pageNo = 1;    } else {       if (Integer.parseInt(pageStr) <= 0) {           pageNo = 1;       } else if(pageNo >= tolPage){           pageNo = tolPage;       } else {           pageNo = Integer.parseInt(pageStr);       }    }     */       rs.close();    stmt.close();    conn.close();%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>    <head>       <base href="<%=basePath%>">        <title>My JSP 'ShowArticleTree.jsp' starting page</title>        <meta http-equiv="pragma" content="no-cache">       <meta http-equiv="cache-control" content="no-cache">       <meta http-equiv="expires" content="0">       <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">       <meta http-equiv="description" content="This is my page">       <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->     </head>     <body>       <a href="Post.jsp">增加新話題</a>       <table border="1">           <%=str%>           <%              str = "";              flag = false;           %>       </table>       共<%=tolPage%>頁  第<%=pageNo%>頁       <br>                 <!-- 添加分頁標記 -->        <!-- 分頁第一種顯示方法 -->       <a href="ShowArticleFlat.jsp?pageNo=<%=pageNo - 1%>"> < </a>         <a href="ShowArticleFlat.jsp?pageNo=<%=pageNo + 1%>"> > </a>       <br>       <br>         <!-- 分頁第二種顯示方法 -->       <form name="form1" action="ShowArticleFlat.jsp">           <select name="pageNo" onchange="document.form1.submit()">              <%                  for (int i = 1; i <= tolPage; i++) {              %>              <option value=<%=i%> <%=(pageNo == i) ? "selected" : ""%>>                  第<%=i%>頁                  <%                  }              %>                        </select>       </form>         <!-- 分頁第三種顯示方法 -->       <form name="form2" action="ShowArticleFlat.jsp">           <input type="text" size=4 value=<%=pageNo%> name="pageNo">           <input type="submit" value="GO">       </form>    </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.