您的位置:首页 > Web前端 > JavaScript

[学习小笔记] JSP分页显示数据

2017-02-21 22:27 441 查看
这几天一直在复习JSP相关的知识,昨天重新做了一次JSP分页显示功能,但是因为时间原因没能及时做好记录,今天将这个小知识点补上。
首先我们需要一个连接数据库的帮助类:DbConnection,一个servlet服务器类:AllGoods,一个实体类:Goods,一个操作数据库类:SelectGoodsDB,一个JSP页面:homePage。
工具类和实体类就不具体谢了,主要写一下查询和服务器的处理。


SelectGoodsDB类:

public class SelectGoodsDB {
// 链接数据库对象
private Connection conn = null;
// 执行语句对象
private PreparedStatement ps = null;
// 接受返回的结果集对象
private ResultSet rs = null;
// 每页显示商品的数量
private final int NUM = 3;
List<Goods> list;

// 查询所有的商品信息
public List<Goods> getGoods(int page) {
list = new ArrayList<Goods>();
// 获取数据库的链接
conn = DbConnection.getConnection();
// 定义查询语句(查询语句需要用limit来查询每个页面的数据)
String sql = "SELECT `no`, `name`, address, time, type FROM t_goods LIMIT "
+ (page - 1) * NUM + ", " + NUM + ";";
try {
// 执行查询语句
ps = conn.prepareStatement(sql);
// 接受返回的结果集
rs = ps.executeQuery();
// 循环遍历结果,保存到集合当中
while (rs.next()) {
Goods good = new Goods();
good.setG_no(rs.getString("no"));
good.setG_name(rs.getString("name"));
good.setG_address(rs.getString("address"));
good.setG_time(rs.getString("time"));
good.setG_type(rs.getString("type"));
list.add(good);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DbConnection.closeAll(rs, ps, conn);
}
return list;
}

// 查询计算最大页码数
public int getNums() {
int row = 0;
// 获取数据库的链接
conn = DbConnection.getConnection();
// 定义查询语句
String sql = "SELECT COUNT(no) FROM t_goods;";
try {
// 操作对象
ps = conn.prepareStatement(sql);
// 执行sql语句
rs = ps.executeQuery();
// 保存商品总数
if (rs.next())
row = rs.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DbConnection.closeAll(rs, ps, conn);
}
// 最后这里我们需要对查询到的数据进行一下判断,用三元运算符判断出具体的页码是多少并返回
return row % NUM != 0 ? row / NUM + 1 : row / NUM;
}
}


AllGoods类:

public class AllGoods extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 设置编码格式处理乱码问题
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
// 保存需要查询的页码
int page = 0;
// 用try catch捕获异常,当发生异常时说明是第一次进入到首页,默认页码为1
try {
page = Integer.valueOf(request.getParameter("page"));
} catch (Exception e) {
// TODO: handle exception
page = 1;
}
// 通过页码查询到对应页面显示的数据
List<Goods> list = new SelectGoodsDB().getGoods(page);
// 查询最大页码
int max = new SelectGoodsDB().getNums();
request.setAttribute("goods", list);
request.setAttribute("page", page);
request.setAttribute("max", max);
request.getRequestDispatcher("homePage.jsp").forward(request, response);
}

}


homePage.jsp:

<%@page import="java.util.ArrayList"%>
<%@page import="com.galibaba.entity.Goods"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>首页</title>
</head>
<style>
tr {
height: 40px;
text-align: center;
}
th, td {
border-bottom: 1px solid skyblue;
}
.fanye {
color: blue;
text-decoration: underline;
cursor: pointer;
}
</style>
<body>
<%
// 保存传递过来当前页面的数据
List<Goods> list = (ArrayList<Goods>) request.getAttribute("goods");
%>
<h1 style="text-align: center">商品信息显示</h1>
<table align="center" width="600px" cellpadding="0" cellspacing="0">
<tr>
<th>序号</th>
<th>姓名</th>
<th>时间</th>
<th>地址</th>
<th>类型</th>
</tr>
<%
for(Goods g : list) {
%>
<tr>
<td><%= g.getG_no() %></td>
<td><%= g.getG_name() %></td>
<td><%= g.getG_time() %></td>
<td><%= g.getG_address() %></td>
<td><%= g.getG_type() %></td>
</tr>
<%
}
// 获取到当前显示的页码
int currentPage = Integer.valueOf(request.getAttribute("page").toString());
// 获取到最大页码
int max = Integer.valueOf(request.getAttribute("max").toString());
%>
<tr>
<td colspan="5">
<a href="goods?page=1">首页</a>
<a href="goods?page=<%= currentPage == 1 ? 1 : currentPage - 1%>">上一页</a>
  ${page} / ${max}   
<a href="goods?page=<%= currentPage == max ? max : currentPage + 1%>">下一页</a>
<a href="goods?page=${max}">尾页</a>
</td>
</tr>
</table>
</body>
</html>


这样,分页查询的功能就可以实现了,主要的代码都在这里了,需要注意的是每次进入的时候都必须从servlet服务器类进入,不能直接从jsp页面进入,不然就会报错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jsp