最好是使用Sql分页,效率比查询出所有然后在List分页要好的多了 Number 每页显示的条数 pg 当前页数 String sql = "select top Number * from bbsTopic where TID not in(select top "+(pg-1)*Number+" TID from bbsTopic)"
可以看看,不过很烂,是sqlserver的<%@ page pageEncoding="GB18030"%> <!-- 分页显示 --> <% final int PAGE_SIZE = 20;//每页显示的帖子数 int pageNo = 1;//页号 String strPageNo = request.getParameter("pageNo"); if(strPageNo != null && !strPageNo.trim().equals("")) { try { pageNo = Integer.parseInt(strPageNo); } catch (NumberFormatException e) { pageNo = 1; } } if(pageNo <= 0) pageNo = 1; int totalPages = 0;//总的页数 List<Article> articles = new ArrayList<Article>(); Connection conn = DB.getConn(); Statement stmtCount = DB.createStmt(conn); ResultSet rsCount = DB.executeQuery(stmtCount, "select count(*) from article where pid = 0"); rsCount.next(); int totalRecords = rsCount.getInt(1); totalPages = (totalRecords + PAGE_SIZE - 1)/PAGE_SIZE; if(pageNo > totalPages) pageNo = totalPages; Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY); int startPos = (pageNo-1) * PAGE_SIZE; //String sql = "select * from article where pid = 0 order by pdate desc limit " + startPos + "," + PAGE_SIZE ; //int ss=PAGE_SIZE+startPos-1; //String sql ="SELECT * FROM ( SELECT TOP "+PAGE_SIZE+" * FROM (SELECT TOP "+ss+" * FROM article order by pdate desc) t1) t2 Order by pdate asc"; //System.out.println(sql); String sql="select * from article where pid=0 order by pdate desc"; ResultSet rs = DB.executeQuery(stmt, sql); if(totalPages>0){ //将记录指针定位到待显示页的第一条记录上 rs.absolute(startPos + 1); int i = 0; while(i<PAGE_SIZE && !rs.isAfterLast()){ Article a = new Article(); a.initFromRs(rs); articles.add(a); rs.next(); i++; } } DB.close(rsCount); DB.close(stmtCount); DB.close(rs); DB.close(stmt); DB.close(conn); %> <div class="jive-buttons"> <table summary="Buttons" border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td class="jive-icon"><a href="post.jsp"> <img src="images/post-16x16.gif" alt="发表新主题" border="0" height="16" width="16"></a></td> <td class="jive-icon-label"><a id="jive-post-thread" href="post.jsp" target="_blank">发表新主题</a></td> </tr> </tbody> </table> </div> <table border="0" cellpadding="3" cellspacing="0" style="width: 100%; height: 30px;"> <tbody> <tr >
Number 每页显示的条数
pg 当前页数
String sql = "select top Number * from bbsTopic where TID not in(select top "+(pg-1)*Number+" TID from bbsTopic)"
<!-- 分页显示 -->
<%
final int PAGE_SIZE = 20;//每页显示的帖子数
int pageNo = 1;//页号
String strPageNo = request.getParameter("pageNo");
if(strPageNo != null && !strPageNo.trim().equals("")) {
try {
pageNo = Integer.parseInt(strPageNo);
} catch (NumberFormatException e) {
pageNo = 1;
}
}
if(pageNo <= 0) pageNo = 1;
int totalPages = 0;//总的页数
List<Article> articles = new ArrayList<Article>();
Connection conn = DB.getConn();
Statement stmtCount = DB.createStmt(conn);
ResultSet rsCount = DB.executeQuery(stmtCount, "select count(*) from article where pid = 0");
rsCount.next();
int totalRecords = rsCount.getInt(1);
totalPages = (totalRecords + PAGE_SIZE - 1)/PAGE_SIZE;
if(pageNo > totalPages) pageNo = totalPages;
Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
int startPos = (pageNo-1) * PAGE_SIZE;
//String sql = "select * from article where pid = 0 order by pdate desc limit " + startPos + "," + PAGE_SIZE ;
//int ss=PAGE_SIZE+startPos-1;
//String sql ="SELECT * FROM ( SELECT TOP "+PAGE_SIZE+" * FROM (SELECT TOP "+ss+" * FROM article order by pdate desc) t1) t2 Order by pdate asc";
//System.out.println(sql);
String sql="select * from article where pid=0 order by pdate desc";
ResultSet rs = DB.executeQuery(stmt, sql);
if(totalPages>0){
//将记录指针定位到待显示页的第一条记录上
rs.absolute(startPos + 1);
int i = 0;
while(i<PAGE_SIZE && !rs.isAfterLast()){
Article a = new Article();
a.initFromRs(rs);
articles.add(a);
rs.next();
i++;
}
}
DB.close(rsCount);
DB.close(stmtCount);
DB.close(rs);
DB.close(stmt);
DB.close(conn);
%>
<div class="jive-buttons">
<table summary="Buttons" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="jive-icon"><a href="post.jsp"> <img src="images/post-16x16.gif" alt="发表新主题" border="0" height="16" width="16"></a></td>
<td class="jive-icon-label"><a id="jive-post-thread" href="post.jsp" target="_blank">发表新主题</a></td>
</tr>
</tbody>
</table>
</div>
<table border="0" cellpadding="3" cellspacing="0" style="width: 100%; height: 30px;">
<tbody>
<tr >
<td>
<span class="nobreak"> 共<%=totalPages %>页,第<%=pageNo %>页- <span class="jive-paginator"> [</span></span>
<span class="nobreak"><span class="jive-paginator">
<a href="articleFlat.jsp?pageNo=1">第一页</a></span></span>
<span class="nobreak"><span class="jive-paginator">|</span></span>
<span class="nobreak"><span class="jive-paginator">
<a href="articleFlat.jsp?pageNo=<%=pageNo - 1 %>">上一页</a>
</span></span>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="99%"><div class="jive-thread-list">
<div class="jive-table">
<table summary="List of threads" cellpadding="0" cellspacing="0" width="100%">
<thead>
<tr>
<th class="jive-first" colspan="3"> 主题 </th>
<th class="jive-author"> <nobr> 作者
</nobr> </th>
<th class="jive-view-count"> <nobr> 浏览
</nobr> </th>
<th class="jive-msg-count" nowrap="nowrap"> 回复 </th>
<th class="jive-last" nowrap="nowrap"> 最新帖子 </th>
</tr>
</thead>
<tbody>
<%
int lineNo = 0;
for(Iterator<Article> it = articles.iterator(); it.hasNext(); ) {
Article a = it.next();
String classStr = lineNo%2 == 0 ? "jive-even" : "jive-odd";
%>
<tr class="<%=classStr %>">
<td class="jive-first" nowrap="nowrap" width="1%"><div class="jive-bullet"> <img src="images/read-16x16.gif" alt="已读" border="0" height="16" width="16">
<!-- div-->
</div></td>
<td nowrap="nowrap" width="1%">
<%
String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
url += request.getContextPath();
url += request.getServletPath();
url += request.getQueryString() == null ? "" : ("?" + request.getQueryString());
//System.out.println(url);
//System.out.println(request.getRequestURI());
//System.out.println(request.getRequestURL());
%>
<%if (admin) {%>
<a href="modify.jsp?id=<%=a.getId()%>">修改</a>
<a href="delete.jsp?id=<%=a.getId()%>&isLeaf=<%=a.isLeaf()%>&pid=<%=a.getPid() %>&from=<%=url %>" onClick="return confirm('你确定要删除?')">删除</a>
<%} %>
</td>
<td class="jive-thread-name" width="95%"><a id="jive-thread-1" href="articleDetailFlat.jsp?id=<%=a.getId() %>&title=<%=a.getTitle() %>&url=<%=url %>&pageNo=1" target="_blank"><%=a.getTitle() %></a></td>
<td class="jive-author" nowrap="nowrap" width="1%"><span class=""> <a href="info.jsp?username=<%=a.getUsername() %>" target="_blank"><%=a.getUsername() %></a> </span></td>
<td class="jive-view-count" width="1%"> <%=a.getNum() %></td>
<td class="jive-msg-count" width="1%"> <%=a.getReploycount() %></td>
<td class="jive-last" nowrap="nowrap" width="1%"><div class="jive-last-post"> <%=new java.text.SimpleDateFormat("yyyy-MM-dd").format(a.getLastDate()) %> <br>
by: <a href="info.jsp?username=<%=a.getName() %>" target="_blank"><%=a.getName() %> »</a> </div></td>
</tr>
<%
lineNo++;
}
%>
</tbody>
</table>
</div>
</div>
<div class="jive-legend"></div></td>
</tr>
</tbody>
</table></body>
</html>