Jsp 分页

<%@ 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>

 效果图如下 

                
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值