您的位置:首页 > 编程语言 > Java开发

Java实现简单分页查询显示

2017-12-05 15:29 447 查看
分页效果图:



实现思路:

数据访问层

传入每页想显示的数据,得到最大页数

// 获取最大页数

public int getMaxPage(String username, int num) {
int result = 0;// 查询结果

int PageNum = 0;

// 得到数据库连接
Connection conn = DBConnection.getConnection();

// 聚合查询得到需要显示的数据

String sql = "select count(*) as count from book where user=? or user=?";

try {
// 预编译
PreparedStatement ps = conn.prepareStatement(sql);

ps.setString(1, username);

ps.setString(2, "");

ResultSet rs = ps.executeQuery();

while (rs.next()) {
result = rs.getInt("count");
}
// 防止算术异常
if (num == 0) {

PageNum = 1;

return PageNum;

}
// 能够整除情况
if (result % num == 0) {

PageNum = result / num;

} else {

PageNum = result / num + 1;
}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return PageNum;

}
传入要显示的页数以及每页记录数得到数据

// 获取分页数据

public List<Book> Limit(String username, int page, int num) {

List<Book> list = new ArrayList<Book>();

// 得到数据库连接
Connection conn = DBConnection.getConnection();

String sql = "select * from book where user=? or user=? limit ?,?";

try {
PreparedStatement ps = conn.prepareStatement(sql);

ps.setString(1, username);

ps.setString(2, "");

ps.setInt(3, (page - 1) * num);// (查询页数-1)*每页记录数
ps.setInt(4, num);

// 查询得到结果集

ResultSet rs = ps.executeQuery();

while (rs.next()) {

int id = rs.getInt("id");

String name = rs.getString("name");

double price = rs.getDouble("price");

String info = rs.getString("info");

String img_url = rs.getString("img_url");

String borrow_date = rs.getString("borrow_date");

String user = rs.getString("user");

Book book = new Book(id, name, price, info, img_url,
borrow_date, user);

list.add(book);

}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStac
4000
kTrace();
}

return list;

}

业务逻辑层

准备好方法:

// 分页得到共多少页

public int getMaxPage(String username, int num) {

return bdao.getMaxPage(username, num);

}

// 分页数据
public List<Book> Limit(String username, int page, int num) {

List<Book> list = new ArrayList<Book>();

list = bdao.Limit(username, page, num);

return list;

}

控制层

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

// 得到登录的用户名

HttpSession session = request.getSession();

String username = session.getAttribute("username").toString();
//分页每页记录数

int num=3;
//得到分页的最大页数,每页3条数据

int PageNum=bservice.getMaxPage(username, num);

//得到请求的页数

int page=Integer.parseInt(request.getParameter("page"));

//准备数据
List<Book> list = bservice.Limit(username, page, num);

request.setAttribute("booklist", list);

request.setAttribute("PageNum", PageNum);

request.setAttribute("page", page);

// 转发
request.getRequestDispatcher("WEB-INF/jsp/main.jsp").forward(request,
response);

}

视图层

<table width="700" border="1" align="center" cellpadding="1"
cellspacing="0">
<tr align="center" bgcolor="#CCFFFF">
<td><a href="listbook.do?page=1">首页</a></td>
<!-- 显示第一页数据时,上页不可点击 -->
<c:choose>

<c:when test="${page ==1 }">

<td>上页</td>
</c:when>

<c:otherwise>

<td><a href="listbook.do?page=${page-1 }"> 上页</a></td>

</c:otherwise>
</c:choose>

<!-- 显示最后一页数据时,下页不可点击 -->
<c:choose>

<c:when test="${page ==PageNum }">

<td>下页</td>
</c:when>

<c:otherwise>

<td><a href="listbook.do?page=${page+1 }"> 下页</a></td>

</c:otherwise>
</c:choose>

<td><a href="listbook.do?page=${PageNum }">末页</a></td>

<td>跳转到第 <input name="page" type="text" id="page" size="1"
value="${page }" /> 页 <input type="button" name="turn" id="button2"
value="确定" onclick="turn(${PageNum})" /></td>

<td>${page }/${PageNum}页</td>
</tr>
</table>

<script type="text/javascript">
function turn(maxpage) {

var index = document.getElementById("page").value;

/* 当输入页数为0或负数时,显示第一页 */

if (index <= 0) {

index = 1;

}
/* 当输入页数大于等于最后一页时,显示最后一页 */

if(index>=maxpage){

index=maxpage;
}

window.location.href = 'listbook.do?page=' + index;

}
</script>

登录成功后,默认显示第一页数据

// 转发到登录成功页面

request.getRequestDispatcher("listbook.do?page=1").forward(request,
response);


更多精彩内容欢迎微信扫码关注,非常感谢您的支持~








内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: