分页show.jsp
<%--
分页操作
<a href="serlect_all?pageindex=${pu.present_page_num>1?pu.present_page_num-1:1}">上一页</a>
<c:forEach var="i" begin="1" end="${pu.total_page_num}">
<a href="serlect_all?pageindex=${i}">${i}</a>
</c:forEach>
<a href="serlect_all?pageindex=${pu.present_page_num+1}">下一页</a>
--%>
pageUtils
package com.zym.utils;
public class pageUtils {
private int total_page_num;
private int present_page_num;
private int num_of_onepage;
private int total_num;
public int getTotal_num() {
return total_num;
}
public void setTotal_num(int total_num) {
this.total_num = total_num;
}
public pageUtils() {
}
public pageUtils(int total_page_num, int present_page_num, int num_of_onepage, int total_num) {
this.total_page_num = total_page_num;
this.present_page_num = present_page_num;
this.num_of_onepage = num_of_onepage;
this.total_num = total_num;
}
public int getTotal_page_num() {
System.out.println(total_page_num+"=================");
return total_page_num;
}
public void setTotal_page_num(int total_page_num) {
this.total_page_num = total_page_num;
}
public int getPresent_page_num() {
return present_page_num;
}
public void setPresent_page_num(int present_page_num) {
this.present_page_num = present_page_num;
}
public int getNum_of_onepage() {
return num_of_onepage;
}
public void setNum_of_onepage(int num_of_onepage) {
this.num_of_onepage = num_of_onepage;
}
@Override
public String toString() {
return "pageUtils{" +
"total_page_num=" + total_page_num +
", present_page_num=" + present_page_num +
", num_of_onepage=" + num_of_onepage +
'}';
}
// public
}
UserDao
public List<User> SelectAll(pageUtils pu) throws SQLException, ClassNotFoundException {
List<User> users = new ArrayList<>();
conn = jdbcUtils.get_conn();
pstt = conn.prepareStatement("select * from user limit ?,? ");
// pstt = conn.prepareStatement("show tables ");
// pstt.set;
pstt.setInt(1,((pu.getPresent_page_num()-1)*pu.getNum_of_onepage()));
pstt.setInt(2, pu.getNum_of_onepage());
rs = pstt.executeQuery();
User u =null;
while (rs.next()){
u = new User(rs.getInt("id"),rs.getString("name"),rs.getInt("age"));
users.add(u);
}
jdbcUtils.close_all(conn,pstt,rs);
return users;
}
servlet
try {
UserDao userDao = new UserDao();
//总页数
//总条数
//当前页数
//一页有多少条
//总条数
int total_num = userDao.select_count();
System.out.println("总条数"+total_num);
//总页数
int total_page_num = 2;
//当前页数默认为1
int present_page_num =1;
if (request.getParameter("pageindex")!=null) {
present_page_num=Integer.parseInt(request.getParameter("pageindex"));
System.out.println(present_page_num+"1111111");
}
//一页有多少条
int num_of_onepage = total_num/total_page_num;
pageUtils pageUtils_obj = new pageUtils(total_page_num,present_page_num,num_of_onepage,total_num);
System.out.println(pageUtils_obj.getTotal_page_num()+"---------------");
request.setAttribute("pu", pageUtils_obj);
List<User> users = userDao.SelectAll(pageUtils_obj);
// boolean res = new UserDao().crete_table();
// System.out.println(res+"=======================");
request.setAttribute("users",users );
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
request.getRequestDispatcher("show.jsp").forward(request,response);
// }