<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>分页显示</title>
</head>
<body>
<center>
<h1>人员列表</h1>
<hr>
<br>
<%!
final String jspUrl = "list_person_false_05.jsp" ;
%>
<%
// 定义如下分页变量
// 1、定义没页要显示的记录数
int lineSize = 10 ;
// 2、定义一个当前是第几页
int currentPage = 1 ;
// 计算出总页数
int pageSize = 0 ;
// 总记录数 / 每页显示的记录数
int allRecorders = 30 ;
%>
<%
// 接收传过来的当前页
try
{
currentPage = Integer.parseInt(request.getParameter("cp")) ;
}
catch(Exception e)
{}
%>
<%
final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
final String DBURL = "jdbc:mysql://localhost/oa" ;
final String DBUSER = "root" ;
final String DBPASSWORD = "root" ;
Connection conn = null ;
%>
<%
try
{
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
PreparedStatement pstmt = null ;
String sql = "SELECT COUNT(id) from person" ;
pstmt = conn.prepareStatement(sql) ;
ResultSet rs = pstmt.executeQuery() ;
if(rs.next())
{
allRecorders = rs.getInt(1) ;
}
rs.close() ;
pstmt.close() ;
// 计算总页数
pageSize = (allRecorders+lineSize-1)/lineSize ;
sql = "SELECT id,uid,name,password FROM person" ;
pstmt = conn.prepareStatement(sql) ;
rs = pstmt.executeQuery() ;
%>
<script language="javaScript">
function openPage(curpage)
{
document.spage.cp.value = curpage ;
// alert(cupage) ;
document.spage.submit() ;
}
function selOpenPage()
{
document.spage.cp.value = document.spage.selpage.value ;
document.spage.submit() ;
}
</script>
<form name="spage" action="<%=jspUrl%>">
<input type="button" value="首页" onClick="openPage(1)" <%=currentPage==1?"disabled":""%>>
<input type="button" value="上一页" onClick="openPage(<%=currentPage-1%>)" <%=currentPage==1?"disabled":""%>>
<input type="button" value="下一页" onClick="openPage(<%=currentPage+1%>)" <%=currentPage==pageSize?"disabled":""%>>
<input type="button" value="尾页" onClick="openPage(<%=pageSize%>)" <%=currentPage==pageSize?"disabled":""%>>
<input type="hidden" name="cp" value="">
<font color="red" size="5"><%=currentPage%></font>
/
<font color="red" size="5"><%=pageSize%></font>
跳转到
<select name="selpage" onChange="selOpenPage()">
<%
for(int x=1;x<=pageSize;x++)
{
%>
<option value="<%=x%>" <%=currentPage==x?"selected":""%>><%=x%></option>
<%
}
%>
</select>
页
</form>
<table border="1" width="80%">
<tr>
<td>编号</td>
<td>登陆名称</td>
<td>姓名</td>
<td>密码</td>
<td colspan="2">操作</td>
</tr>
<%
int i = 0 ;
for(int x=0;x<(currentPage-1)*lineSize;x++)
{
rs.next();
}
// 对于输出代码之前要求按显示的页数空出
for(int x=0;x<lineSize;x++)
{
if(rs.next())
{
i++ ;
int id = rs.getInt(1) ;
String userid = rs.getString(2) ;
String name = rs.getString(3) ;
String password = rs.getString(4) ;
%>
<tr>
<td><%=id%></td>
<td><%=userid%></td>
<td><%=name%></td>
<td><%=password%></td>
<td>更新</td>
<td>删除</td>
</tr>
<%
}
}
rs.close() ;
pstmt.close() ;
if(i==0)
{
%>
<tr>
<td colspan="6">没有任何数据!!</td>
</tr>
<%
}
%>
</table>
<%
}
catch(Exception e)
{
%>
<h2>系统出错!!!</h2>
<%
}
finally
{
conn.close() ;
}
%>
</center>
</body>
</html>