<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>分頁例項</title>
</head>
<body bgcolor="CCBBDD">
<center>
<h3>分頁顯示記錄內容</h3>
<hr>
<table border="1" width="50%" bgcolor="cccfff" align="center">
<tr>
<th width="87" align="center">編號</th>
<th width="87" align="center">姓名</th>
<th width="87" align="center">密碼</th>
<th width="87" align="center">性別</th>
<th width="87" align="center">家鄉</th>
<th width="87" align="center">資訊</th>
</tr>
<%
Class.forName("com.mysql.jdbc.Driver");
String url3="jdbc:mysql://localhost:3306/test?serverTimezone=UTC&autoReconnect=true&useSSL=false&characterEncoding=utf8";
Connection conn3 = DriverManager.getConnection(url3, "root","123456");
int intPageSize;
int intRowCount;
int intPageCount;
int intPage;
String strPage;
int i;
intPageSize=2;
strPage=request.getParameter("page");
if(strPage==null){
intPage=1;
}else{
intPage=java.lang.Integer.parseInt(strPage);
if(intPage<1)
intPage=1;
}
Statement stmt=conn3.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY
);
String sql="SELECT * FROM user";
ResultSet rs=stmt.executeQuery(sql);
rs.last();
intRowCount=rs.getRow();
intPageCount=(intRowCount+intPageSize-1)/intPageSize;
if(intPage>intPageCount)
intPage=intPageCount;
if(intPageCount>0){
rs.absolute((intPage-1)*intPageSize+1);
i=0;
while(i<intPageSize&& !rs.isAfterLast()){
%>
<tr>
<td><%=rs.getString("id") %></td>
<td><%=rs.getString("name") %></td>
<td><%=rs.getString("pwd") %></td>
<td><%=rs.getString("sex") %></td>
<td><%=rs.getString("home") %></td>
<td><%=rs.getString("info") %></td>
</tr>
<%
rs.next();
i++;
}
}
%>
</table>
<hr>
<div align="center">
第<%=intPage %>頁 共<%=intPageCount %>頁
<%
if(intPage<intPageCount){
%>
<a href="pagebreak.jsp?page=<%=intPage+1 %>">下一頁</a>
<%
}
if(intPage>1){
%>
<a href="pagebreak.jsp?page=<%=intPage-1 %>">上一頁</a>
<%
}
rs.close();
stmt.close();
conn3.close();
%>
</div>
</center>
</body>
</html>