日期:2014-05-17 浏览次数:20815 次
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="gbk"%>
<%
int pageSize = 3;
String strPageNo = request.getParameter("pageNo");
int pageNo;
if(strPageNo ==null || strPageNo.equals("")) {
pageNo = 1;
} else {
try {
pageNo = Integer.parseInt(strPageNo.trim());
} catch (NumberFormatException e) {
pageNo = 1;
}
if(pageNo <= 0) {
pageNo = 1;
}
}
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/bbsa", "root", "root");
Statement stmtCount = conn.createStatement();
ResultSet rsCount = stmtCount.executeQuery("select count(*) from article where pid = 0");
rsCount.next();
int totalRecords = rsCount.getInt(1);
int totalPages = totalRecords % pageSize == 0 ? totalRecords / pageSize : totalRecords / pageSize + 1;
if(pageNo > totalPages) {
pageNo = totalPages;
}
int startPos = (pageNo - 1) * pageSize;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from article where pid = 0 order by pdate desc limit " + startPos + "," + pageSize);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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="main.css">
</head>
<body>
<a href="Post.jsp">发表新帖</a><br/>
<table border="1">
<%
while(rs.next()) {
%>
<tr>
<td>
<%=rs.getString("title") %>
</td>
</tr>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</table>
<br/>共<%=totalPages %>页 第<%=pageNo %>页
<a href="ShowArticleFlat.jsp?pageNo=<%=pageNo == 1 ? 1 : pageNo - 1 %>">上一页</a>
<a href="ShowArticleFlat.jsp?pageNo=<%=1 %>">首页</a>
<a href="ShowArticleFlat.jsp?pageNo=<%=pageNo - 5 > 0 ? pageNo - 5 : pageNo %>">前五页</a>
<%
int pageArticles = 5;
int pageRows = totalPages % pageArticles == 0 ? totalPages / pageArticles : totalPages / pageArticles + 1;
//System.out.println(pageRows);
%>
<%
int k = pageNo % pageArticles == 0 ? (pageNo / pageArticles) * pageArticles - 4 : (pageNo / pageArticles) * pageArticles + 1;
int m = totalPages - pageNo < 1 ? k + (totalPages - pageNo) : k + 4;
for(int j = k; j <= m; j++) {
%>
<a href="?pageNo=<%=j %>" <%=(pageNo == j) ? "class='bgOn'" : ""