分页
public class findByPageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String current = request.getParameter("currentPage");
String size1 =request.getParameter("size");
int currentPage = Integer.parseInt(current);
int size = Integer.parseInt(size1);
IUserService userService = new UserService();
PageInfo pageInfo = userService.findByPage(currentPage,size);
HttpSession session=request.getSession();
session.setAttribute("user1",pageInfo);
request.getRequestDispatcher("allUser.jsp").forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int current = 1;
int size1 =5;
IUserService userService = new UserService();
PageInfo pageInfo = userService.findByPage(current,size1);
HttpSession session=request.getSession();
session.setAttribute("user1",pageInfo);
request.getRequestDispatcher("allUser.jsp").forward(request, response);
}
}
public PageInfo findByPage(int currentPage, int size) {
PageInfo pageInfo = new PageInfo();
pageInfo.setCurrentPage(currentPage);
pageInfo.setSize(size);
int start =(currentPage-1)*size;
List<User> users =userDao.findByPage(start,size);
pageInfo.setList(users);
int count = userDao.count();
pageInfo.setTotalCount(count);
Double c =Double.valueOf(count);
Double ceil = Math.ceil(c/size);
pageInfo.setTotalPage(ceil.intValue());
return pageInfo;
public List<User> findByPage(int start, int size) {
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
List<User> users = new ArrayList<>();
try {
connection = DBUtill.getConnection();
String sql = "select * from fuser limit ?,?";
statement = connection.prepareStatement(sql);
statement.setInt(1,start);
statement.setInt(2,size);
resultSet = statement.executeQuery();
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt(1));
user.setName(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
users.add(user);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtill.closeAll(resultSet, statement, connection);
}
return users;
}
@Override
public int count() {
ResultSet resultSet = null;
PreparedStatement statement = null;
Connection connection = null;
int count = 0;
try {
connection = DBUtill.getConnection();
String sql = "SELECT COUNT(*) FROM fuser";
statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();
while (resultSet.next()){
count = resultSet.getInt(1);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtill.closeAll(resultSet, statement, connection);
}
return count;
}
package com.gm.pojo;
import com.gm.Model.User;
import java.util.List;
public class PageInfo {
private int currentPage;
private int size;
private List<User> list;
private int totalPage;
private int totalCount;
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totaLCount) {
this.totalCount = totaLCount;
}
@Override
public String toString() {
return "PageInfo{" +
"currentPage=" + currentPage +
", size=" + size +
", list=" + list +
", totalPage=" + totalPage +
", totaLCount=" + totalCount +
'}';
}
}