代碼寫在了下面,可以將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>