jsp(标签)+javabean+servlet+dao分页

----------------------------------------------------------------------
jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.qxm.temp.PageControl;"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>display</title>
</head>
<body>
员工信息:
<hr>
<table bordercolor="red" border="1">
<tr>
<td>
员工id
</td>
<td>
姓名
</td>
<td>
班级
</td>
<td>
邮箱
</td>
</tr>
<c:forEach var="smalllist" items="${pc.smallList}">
<tr>
<td>
<c:out value="${smalllist.id}" />
</td>
<td>
<c:out value="${smalllist.name}" />
</td>
<td>
<c:out value="${smalllist.gender}" />
</td>
<td>
<c:out value="${smalllist.email}" />
</td>
</tr>
</c:forEach>
</table>
<a href="Dis?pageindex=1">首页</a>
<c:if test="${pc.previousPageCount > 0}" var="true">
<a href="Dis?pageindex=${pc.previousPageCount}">上一页</a>
</c:if>
<c:if test="${ pc.nextPagecount <= pc.pageSize}" var="true">
<a href="Dis?pageindex=${pc.nextPagecount}">下一页</a>
</c:if>
<a href="Dis?pageindex=${pc.pageSize}">尾页</a>
</body>
</html>

----------------------------------------------------------------------
po

package com.qxm.po;

import java.io.Serializable;

public class Student implements Serializable {

private static final long serialVersionUID = 1L;

private int id;

private String name;

private String gender;

private String email;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

}

----------------------------------------------------------------------
servlet

package servlet;

import java.io.IOException;
import java.util.ArrayList;

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

import com.qxm.dao.ConnectionDB;
import com.qxm.po.Student;
import com.qxm.temp.PageControl;

@SuppressWarnings("serial")
public class Dis 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 {

String currentpage = request.getParameter("pageindex");
if(currentpage == null){
currentpage = "1";
}
int pageindex = Integer.parseInt(currentpage);
PageControl pc = (PageControl)request.getAttribute("pc");
if(pc == null){
pc = new PageControl();
ConnectionDB condb = new ConnectionDB();
ArrayList<Student> allList = condb.getResultSet();
pc.setBigList(allList);
request.setAttribute("pc", pc);
}
pc.setCurrentPage(pageindex);
this.getServletContext().getRequestDispatcher("/display.jsp").forward(request, response);
}
}

----------------------------------------------------------------------
dao层

package com.qxm.dao;

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 com.qxm.po.Student;
@SuppressWarnings("unchecked")
public class ConnectionDB {

public static Connection conn = null;
public static PreparedStatement ps = null;
public static ResultSet rs = null;

public void getconnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/info",
"root", "root");
} catch (ClassNotFoundException e) {
System.out.println("数据库驱动找不到");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("数据库连接异常");
e.printStackTrace();
}
}

/**
* id name gender email
* @return
*/

public ArrayList<Student> getResultSet(){
this.getconnection();
ArrayList<Student> list = null;
list = new ArrayList<Student>();

String sql = "select id,name,gender,email from student";
try {
rs = conn.prepareStatement(sql).executeQuery();
while(rs.next()){
Student stu = new Student();
stu.setId(rs.getInt(1));
stu.setName(rs.getString(2));
stu.setGender(rs.getString(3));
stu.setEmail(rs.getString(4));
list.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
this.closeConnection();
}
return list;
}

public void closeConnection(){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
System.out.println("rs关闭异常");
e.printStackTrace();
}
rs = null;
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
System.out.println("ps关闭异常");
e.printStackTrace();
}
ps = null;
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
System.out.println("conn关闭异常");
e.printStackTrace();
}
}
conn = null;
}
}


----------------------------------------------------------------------
分页逻辑

package com.qxm.temp;

import java.util.ArrayList;

import com.qxm.po.Student;

public class PageControl {

private ArrayList<Student> bigList; // 总集合
private int currentPage = 1; // 当前页数
private int pageCount = 2; // 每页数据的条数
private int pageSize; // 总页数
private int valueCount; // 总数据的条数
private ArrayList<Student> smallList;// 分页集合
private int previousPageCount;// 上一页的页数
private int nextPagecount; // 下一页的页数

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
// 上一页
previousPageCount = currentPage - 1;
// 下一页
nextPagecount = currentPage + 1;
smallList = new ArrayList<Student>();
for (int i = (currentPage - 1) * pageCount; i < currentPage * pageCount
&& i < valueCount; i++) {
smallList.add(bigList.get(i));
}
}

public void setBigList(ArrayList<Student> bigList) {
this.bigList = bigList;
valueCount = bigList.size();
pageSize = valueCount % pageCount == 0 ? valueCount / pageCount
: valueCount / pageCount + 1;
}

public int getCurrentPage() {
return currentPage;
}

public ArrayList<Student> getBigList() {
return bigList;
}

public int getPageCount() {
return pageCount;
}

public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getValueCount() {
return valueCount;
}

public void setValueCount(int valueCount) {
this.valueCount = valueCount;
}

public ArrayList<Student> getSmallList() {
return smallList;
}

public void setSmallList(ArrayList<Student> smallList) {
this.smallList = smallList;
}

public int getPreviousPageCount() {
return previousPageCount;
}

public void setPreviousPageCount(int previousPageCount) {
this.previousPageCount = previousPageCount;
}

public int getNextPagecount() {
return nextPagecount;
}

public void setNextPagecount(int nextPagecount) {
this.nextPagecount = nextPagecount;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值