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>
分享到:
相关推荐
本文实例讲述了bootstrap-table后端分页功能。分享给大家供大家参考,具体如下: 使用bootstrap-table实现后台分页 插件资源点击此处本站下载。 引用以下css <link rel="stylesheet" href="../plugins/...
Jsp中利用JavaBean技术实现分页功能 Jsp中利用JavaBean技术实现分页功能
实现分页功能,实现首页,下一页,上一页,末页的功能
主要介绍了Python的Flask框架中实现分页功能的教程,文中的示例基于一个博客来实现,需要的朋友可以参考下
mui官网下载的mui.js,方便以后在引用mui插件的时候使用,可以实现模拟app的分页功能。
此代码是采用AJAX技术 实现GridView的分页功能,帮助初学AJAX的学习者有很好的帮助
java 分页功能实现,也代码参考,配有详细的注释
struts spring hibernate 登陆 SSH整合分页功能 SSH整合分页struts spring hibernate 登陆 SSH整合分页功能 SSH整合分页struts spring hibernate 登陆 SSH整合分页功能 SSH整合分页
js分页功能 js分页功能js分页功能 js分页功能js分页功能 js分页功能
SSM(Mybatis、Spring、SpringMVC)整合实现增删改查及分页功能
使用AspNetPager控件实现ASP.NET分页功能 “控件+源代码”!!
SSH 基础上 分页功能 原理讲解及实现
C# DataGridView分页功能的实现
这是个简单的分页功能,为初学ssh框架的程序员提供解决方案
使用SpringBoot整合Mybatis和Thymeleaf实现分页功能,,自定义Page
通过servlet以及JDBC等知识点,实现分页功能,满足分页需求!
一个很好用的jquery实现的分页功能!!!实现不刷新页面的分页功能
实现简单的分页功能,用Struts 和hibernate实现
jsp分页功能,对于网页制作者来说,非常方便实用。
javaweb中我们常常用到分页功能,这里我写了一个小例子,供大家参考,