<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<jsp:useBean id="DBConnection" scope="page" class="com.DBConnection" />
<jsp:useBean id="userInfo" scope="session" class="com.UserInfo" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用戶信息列表</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>
<!-- 讀取數據庫中的用戶信息分頁顯示在web頁面上-->
<%
String sql = "select * from UserInfo order by userid asc";
int currentPage = 1; //當前頁號,初始頁號為1
int totalPages = 0; //總頁數
int pageSize = 10; //每頁顯示的記錄數
ResultSet rs = DBConnection.executeQuery(sql);
rs.last();
int lastRow = rs.getRow();
totalPages = (lastRow%pageSize==0)?(lastRow/pageSize):(lastRow/pageSize+1); //計算分頁后的總頁數
%>
<%--用戶通過表單提交想要顯示的頁碼數--%>
<FORM action="displayUserInfo.jsp" method=get>
輸入頁碼數:<INPUT type="text" name="showPage" value="1" size=4>
<INPUT type="submit" name="go" value="GO">
</FORM>
<P>
共有<%=totalPages%>頁記錄
<BR>每頁顯示<%=pageSize%>條記錄
<%--get requested page--%>
<%
String requestPage = request.getParameter("showPage");
if(requestPage==null){
requestPage = "1";
}
try{
currentPage = Integer.parseInt(requestPage);
}
catch(NumberFormatException e){
currentPage = 1;
}
if(currentPage<=1){
currentPage =1;
}
if(currentPage>=totalPages){
currentPage = totalPages;
}
%>
<%--end of get requested page--%>
<BR>目前顯示第<%=currentPage%>頁
<%
//光標移動到要顯示頁的第一行position
int position = (currentPage-1)*pageSize+1;
rs.absolute(position); //設置游標
out.print("光標定位到這里!"+position);
//構造用戶信息表格
out.print("<table border>");
out.print("<tr>");
out.print("<th>姓名</th>");
out.print("<th>性別</th>");
out.print("<th>密碼</th>");
out.print("<th>出生日期</th>");
out.print("<th>電子信箱</th>");
out.print("<th>地址</th>");
out.print("</tr>");
for(int i=1;i<=pageSize;i++){
out.print("<tr>");
out.print("<td>"+rs.getString("userName")+"</td>");
out.print("<td>"+rs.getString("gender")+"</td>");
out.print("<td>"+rs.getString("psw")+"</td>");
out.print("<td>"+rs.getString("birthday")+"</td>");
out.print("<td>"+rs.getString("email")+"</td>");
out.print("<td>"+rs.getString("addr")+"</td>");
out.print("</tr>");
if(rs.next()==false) break;//此處代碼原來為rs.next();
}
out.print("</table>");
rs.close();
//DBConnection.close();
%>
</body>
</html>