jsp-------
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>ID</td>
<td>姓名</td>
<td>手机号</td>
<td>身份证号</td>
<td>时间</td>
<td>性别</td>
</tr>
<c:forEach items="${list }" var="a">
<tr>
<td>${a.aid}</td>
<td>${a.person}</td>
<td>${a.phone}</td>
<td>${a.ipcard}</td>
<td>${a.birthday}</td>
<td>${a.sex}</td>
</tr>
</c:forEach>
<tr>
<td><a href="Web?pageCode=1">首页</a></td>
<td><c:if test="${pageCode==1 }" var="ok"></c:if> <c:if
test="${ok }">
<a href="Web?pageCode=1">上一页</a>
</c:if> <c:if test="${!ok }">
<a href="Web?pageCode=${pageCode-1 }">上一页</a>
</c:if></td>
<td>当前是第${pageCode }页</td>
<td>共${totalCode }页</td>
<td><a href="Web?pageCode=${pageCode+1 }">下一页</a></td>
<td><a href="Web?pageCode=${totalCode }">尾页</a></td>
</tr>
</table>
</body>
</html>
servlet---------------------
package com.a.web;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.a.profession.AppointmentRealize;
import com.a.reserve.Appointment;
@WebServlet("/Web")
public class Web extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/thml;charset=utf-8");
// 每页条数 ,当前页数 ,总页数 ,总条数
int pageSize = 3;
int pageCode = 0;
int totalCode = 0;
int totalCount = 0;
AppointmentRealize app = new AppointmentRealize();
totalCount = app.peoplecounting();
// 计算页数总条数除每页条数就等于总页数,
if (totalCount % pageSize == 0) {
totalCode = totalCount / pageSize;
} else {
//若有余数宁外加一新一页
totalCode = totalCount / pageSize + 1;
}
// 计算当前页
if (request.getParameter("pageCode") == null || request.getParameter("pageCode") == "") {
pageCode = 1;
} else {
//类型转换每页数跟总页数进行对比
if (Integer.parseInt(request.getParameter("pageCode")) > totalCode) {
pageCode = totalCode;
} else {
pageCode = Integer.parseInt(request.getParameter("pageCode"));
}
}
request.setAttribute("totalCode",totalCode );
request.setAttribute("pageCode",pageCode );
List<Appointment> list = app.AppointmentTrue(pageSize,(Integer)request.getAttribute("pageCode"));
request.setAttribute("list", list);
request.getRequestDispatcher("You.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
-----------
package com.a.profession;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.a.reserve.Appointment;
import com.a.util.DBTools;
public class AppointmentRealize implements AppointmentDao {
@Override
public int peoplecounting() {
Connection conn = DBTools.getConn();
int count = 0;
// 计算出一共多少条数据
String sql = "SELECT COUNT(*) FROM appointment";
try {
PreparedStatement pa = conn.prepareStatement(sql);
ResultSet rs = pa.executeQuery();
if (rs.next()) {
count = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
@Override
public List<Appointment> AppointmentTrue(int pageSize, int pageCode) {
ArrayList<Appointment> arrayList = new ArrayList<Appointment>();
Connection conn = DBTools.getConn();
String sql = "select * from appointment limit ?,?";
try {
PreparedStatement pq = conn.prepareStatement(sql);
pq.setInt(1, (pageCode - 1) * pageSize);
pq.setInt(2, pageSize);
ResultSet pw = pq.executeQuery();
while (pw.next()) {
Appointment A = new Appointment();
A.setAid(pw.getInt(1));
A.setPerson(pw.getString(2));
A.setPhone(pw.getString(4));
A.setIpcard(pw.getString(5));
A.setBirthday(pw.getString(6));
A.setVid(pw.getInt(7));
arrayList.add(A);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return arrayList;
}
}
---------
public interface AppointmentDao {
/**
* 得到多少条数据
* @return 一共多少人
*
* */
public int peoplecounting();
/**
* 查询所有人数(分页)
* @param pageSize 每页显示的条数
* @param pageCode 页数
* @return 每页应该显示的人数
* */
public List<Appointment> AppointmentTrue(int pageSize,int pageCode);
--------
package com.a.reserve;
public class Appointment {
private int aid;
private String person;
private String phone;
private String ipcard;
private String birthday;
private String sex;
private int vid;
public Appointment(int aid, String person, String phone, String ipcard, String birthday, String sex, int vid) {
super();
this.aid = aid;
this.person = person;
this.phone = phone;
this.ipcard = ipcard;
this.birthday = birthday;
this.sex = sex;
this.vid = vid;
}
public Appointment() {
super();
}
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getIpcard() {
return ipcard;
}
public void setIpcard(String ipcard) {
this.ipcard = ipcard;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getVid() {
return vid;
}
public void setVid(int vid) {
this.vid = vid;
}
}