`
duoerbasilu
  • 浏览: 1483678 次
文章分类
社区版块
存档分类
最新评论

分页功能

 
阅读更多
package com.jzh.pagecount;


import java.util.List;

import com.jzh.DButil.Employee;

public class PageBean {
	private List<Employee> list;
	private int pageNo;
	private int pageSize;
	private int totalCount;
	private int totalPage;
	public List<Employee> getList() {
		return list;
	}
	public void setList(List<Employee> list) {
		this.list = list;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getTotalPage()
	{
		if(totalCount%pageSize==0)
		{
			return totalCount/pageSize;
		}
		else
		{
			return totalCount/pageSize+1;
		}
	}
	/**
	 * 判断是否有下一页
	 * @return boolean
	 */
	public PageBean(){};
	
	
}
package com.jzh.DButil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jzh.pagecount.PageBean;

public class DBConnection {
	public static Connection getConnection()
	{
		Connection conn=null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String url="jdbc:oracle:thin:@localhost:1521:oracle";
			String uid = "hr";
			String pwd = "hr";
			conn = DriverManager.getConnection(url, uid, pwd);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
		
	}
	public PageBean listEmp(int pageNo,int pageSize)
	{
		int begin=(pageNo-1)*pageSize;
		int end=pageNo*pageSize;
		List<Employee> list=new ArrayList<Employee>();
		Employee emp=null;
		PageBean pb=new PageBean();
		Connection conn=getConnection();
		String sql="select last_name,email from (select ROWNUM id,employees.* from employees )where  id between ? and ?";
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, begin);
			pstmt.setInt(2, end);
			rs=pstmt.executeQuery();
			while(rs.next())
			{
				emp=new Employee();
				emp.setLast_name(rs.getString("last_name"));
				emp.setEmail(rs.getString("email"));
				list.add(emp);
			}
			pb.setList(list);
			pb.setPageNo(pageNo);
			pb.setPageSize(pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			pstmt=conn.prepareStatement("select count(*) from employees");
		    rs=pstmt.executeQuery();
			while(rs.next())
			{
				pb.setTotalCount(rs.getInt(1));
				pb.setTotalPage(rs.getInt(1)/pageSize);
				System.out.println(rs.getInt(1)/pageSize);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			conn.close();
			pstmt.close();
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pb;
	
		
	}
}

package com.jzh.DButil;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jzh.pagecount.PageBean;

public class PageServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageNo=1;
		int pageCount=5;
		String pageNoStr=request.getParameter("pageNo");
		String pageCountStr=request.getParameter("pageCount");
		System.out.println("servlet"+pageNoStr+" "+pageCountStr);
		if(pageNoStr!=null&&pageNoStr!="")
		{
			pageNo=Integer.parseInt(pageNoStr);
			if(pageNo<1)
			{
				pageNo=1;
			}
			
		}
		if(pageCountStr!=null&&pageCountStr!="")
		{
			pageCount=Integer.parseInt(pageCountStr);
		}
		DBConnection db=new DBConnection();
		PageBean pb=db.listEmp(pageNo, pageCount);
		request.setAttribute("pageBean", pb);
		request.getRequestDispatcher("/page.jsp").forward(request, response);
	}

}

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'page.jsp' starting page</title>
    
  </head>
  <body>
   <form action="/DiaryManagerSystem/servlet/PageServlet" method="get">
   	<table border="1">
   		<tr>
   			<td>
   		</tr>
   		<c:forEach items="${pageBean.list}" var="e">
   		<tr>
   			<td>${e.last_name }</td> <td>${e.email }</td>
   		</tr>
   		</c:forEach>
   		<tr>
   			<td colspan="2">跳转到:<input type="text" value="${pageBean.pageNo }"  name="pageNo" size="5"/></td>
   		</tr>
   		<tr>
   			<td colspan="2">每页记录数:<input type="text" name="pageCount" value="${pageBean.pageSize }" size="5" /></td>
   		</tr>
     	<tr>
   			<td><input type=submit value="提交""/></td>
    	</tr>
   		<tr>
   			<td><a href="/DiaryManagerSystem/servlet/PageServlet?pageNo=${pageBean.pageNo-1}">上一页</a></td>
  			<td><a href="/DiaryManagerSystem/servlet/PageServlet?pageNo=${pageBean.pageNo+1}">下一页</a></td>
   			<td><a href="/DiaryManagerSystem/servlet/PageServlet?pageNo=${pageBean.totalPage}">最后一页</a></td>
   		<td>
   		
   		</td>
   		</tr>
   		</table>
   </form>
  </body>
</html>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics