<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<title>testPage</title>
</head>
<body>
<center>
<h1>员工信息</h1>
<hr>
<%!
public static final String DBDRIVER="com.mysql.jdbc.Driver";
public static final String DBURL="jdbc:mysql://192.168.14.30:3306/emp";
public static final String DBUSER="root";
public static final String DBPASS="123456";
public static final int PAGESIZE=5;
public int curPage=1;
public int pageCount;
%>
<%
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet res=null;
%>
<%
try{
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql="select empno,ename,job,sal,hiredate,comm from emhd";
pstmt=conn.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
res=pstmt.executeQuery();
res.last();
int size=res.getRow();
//总的页面数
pageCount=(size/PAGESIZE==0)?(size/PAGESIZE):(size/PAGESIZE+1);
String tmp=request.getParameter("curPage");
//获取当前页面数,固定首页与尾页
if(tmp==null){
tmp="1";
}
curPage=Integer.parseInt(tmp);
if(curPage>=pageCount){
curPage=pageCount;
} else if(curPage<=1){
curPage=1;
}
//rs.absolute(n); 可以将指针跳到第n行。
boolean flag=res.absolute((curPage-1)*PAGESIZE+1);
%>
<table border="1" width="80%">
<tr>
<td>雇员编号</td>
<td>雇员姓名</td>
<td>雇员工作</td>
<td>雇员工资</td>
<td>雇员日期</td>
<td>雇员奖金</td>
</tr>
<%
int count =0;
while(res.next()){
if(count>PAGESIZE) break;
int empno=res.getInt(1);
String ename=res.getString(2);
String job=res.getString(3);
float sal=res.getFloat(4);
java.util.Date date=res.getDate(5);
float comm=res.getFloat(6);
count++;
%>
<tr>
<td><%=empno%></td>
<td><%=ename%></td>
<td><%=job%></td>
<td><%=sal%></td>
<td><%=date%></td>
<td><%=comm%></td>
</tr>
<%
}
} catch(Exception e){
}finally{
if(res!=null){
res.close();
pstmt.close();
conn.close();
}
}
%>
</table>
<a href = "testPage.jsp?curPage=1" >首页</a>
<a href = "testPage.jsp?curPage=<%=curPage-1%>" >上一页</a>
<a href = "testPage.jsp?curPage=<%=curPage+1%>" >下一页</a>
<a href = "testPage.jsp?curPage=<%=pageCount%>" >尾页</a>
第<%=curPage%>页/共<%=pageCount%>页
</center>
</body>
</html>
效果图如下