分页设计的本质就是,分页查询,就是SQL语句当中的(select * from ? limit ? , ?),这里第一个?是所分页的那张表 ,第二个?从哪条开始,第三个?是在页面上想让这张表出现几条数据。
下面先看一下分页所需要的两个函数。1.查询所有表的数据 2.获取当前页数据
/**
* 查询表中所有数据,返回进行渲染
*/
public List<emp> selectAll(String sql) {
ArrayList<emp> emps = new ArrayList<>();
Connection conn = DbLink.getConn();
try {
PreparedStatement statement = conn.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
Integer id = resultSet.getInt("id");
String name = resultSet.getString("name");
String city = resultSet.getString("city");
String country = resultSet.getString("country");
emps.add(new emp(id, name, city, country));
}
} catch (SQLException e) {
e.printStackTrace();
}
return emps;
}
/**
* 获取当前页数据
* @param start 偏移量
* @param pageSize 规定每页的记录数
* @return list 存放所有查询表当中的所有数据
*/
public List<emp> QueryRecords(int start,int pageSize){
Connection conn = DbLink.getConn();
ArrayList<emp> list = new ArrayList<>();
String sql = "select * from emp limit ?,?";
try {
PreparedStatement statement = conn.prepareStatement(sql);
statement.setInt(1,start);
statement.setInt(2,pageSize);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()){
emp emp = new emp();
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String city = resultSet.getString("city");
String country = resultSet.getString("country");
emp.setId(id);
emp.setName(name);
emp.setCity(city);
emp.setCountry(country);
list.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
下面我们看jsp代码,如何去实现
<%@ page import="Dao.handleDb" %>
<%@ page import="Pojo.emp" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>分页设计</title>
</head>
<body>
<%
handleDb handleDb = new handleDb();
//1.规定每页的记录数
int pageSize = 3;
//2.总的sql记录条数 count
List<emp> emps = handleDb.selectAll("select * from emp");
int count = emps.size();
//页的数量
//3.计算总页数
int pageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
//4.当前页
Integer page1;
if (request.getParameter("page1") == null) {
page1 = 1;
} else {
page1 = Integer.valueOf(request.getParameter("page1"));
}
//sql=select * from emp limit start,pageSize;
//5.计算偏移值
int start = (page1 - 1) * pageSize;
//6.获取当前页的数据
List<emp> list = handleDb.QueryRecords(start, pageSize);
%>
<table cellspacing="0" border="1">
<tr>
<th>id</th>
<th>姓名</th>
<th>城市</th>
<th>国家</th>
</tr>
<%
for (int i = 0; i < list.size(); i++) {
%>
<tr>
<td><%=list.get(i).getId()%>
</td>
<td><%=list.get(i).getName()%>
</td>
<td><%=list.get(i).getCity()%>
</td>
<td><%=list.get(i).getCountry()%>
</td>
</tr>
<%
}
%>
</table>
<%
if (page1==1){
%>
<a>首页</a>
<a>上一页</a>
<a href=showList1.jsp?page1=<%=page1 + 1%>>下一页</a>
<a href=showList1.jsp?page1=<%=pageCount%>>尾页</a>
<%
}else if (page1 == pageCount){
%>
<a href=showList1.jsp?page1=1>首页</a>
<a href=showList1.jsp?page1=<%=page1 - 1%>>上一页</a>
<a>下一页</a>
<a>尾页</a>
<%
}else {
%>
<a href=showList1.jsp?page1=1>首页</a>
<a href=showList1.jsp?page1=<%=page1 - 1%>>上一页</a>
<a href=showList1.jsp?page1=<%=page1 + 1%>>下一页</a>
<a href=showList1.jsp?page1=<%=pageCount%>>尾页</a>
<%
}
%>
</body>
</html>
下面我面看下运行效果: