很多初学jsp的小伙伴们,都为数据库的信息如何在jsp上面如何显着急,一次性显示数据过于庞大,不美观,无法突出程序员的设计感,
揭晓来,小编就为各位介绍一种分页查询的实现方法。
第一步建立数据存放的界面
st为servlet返回的结果
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link type="text/css" rel="stylesheet"
href="http://localhost:8080/wust-find/ip/css/index.css" />
</head>
<body>
<div class="table_div">
<div class="div_clear">
<div class="left_top"></div>
<div class="center_top">
<div style="float: left">
<img src="./tab/images/tb.jpg" width="16px" height="16px"
style="vertical-align: middle" /> <span style="font-weight: bold">你当前的位置</span>:[业务中心]-[失物招领]
</div>
<div style="float: right; padding-right: 6px">
<img width='16' height='16'
src="http://localhost:8080/wust-find/ip//tab/images/22.gif"
style="vertical-align: middle" /> <a href="#">新增</a> <img
width='16' height='16'
src="http://localhost:8080/wust-find/ip//tab/images/33.gif"
style="vertical-align: middle" /> <a href="#">修改</a> <img
width='16' height='16'
src="http://localhost:8080/wust-find/ip//tab/images/11.gif"
style="vertical-align: middle" /> <a href="#">删除</a>
</div>
</div>
<div class="right_top"></div>
</div>
<div class="div_clear">
<div class="left_center"></div>
<div class="center_center">
<div class="table_content">
<table cellspacing="0px" cellpadding="0px">
<thead>
<tr>
<th width="16%">标题</th>
<th width="16%">内容</th>
<th width="16%">图片</th>
<th width="20%" style="border-right: none">操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${student}" var="st">
<tr>
<td>${st.getBiaoti()}</td>
<td>${st.getMiaoshu()}</td>
<td><a href="${st.getAdress()}" target="_BLANK">点击查看</a></td>
<td width="20%" style="border-right: none"><img width='16'
height='16'
src="http://localhost:8080/wust-find/ip//tab/images/del.gif"
style="vertical-align: middle" /> <a href="#">修改</a> <img
width='16' height='16'
src="http://localhost:8080/wust-find/ip//tab/images/edt.gif"
style="vertical-align: middle" /> <a href="#">删除</a></td>
</tr>
</c:forEach>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</br>
</tbody>
</table>
</div>
</div>
<div class="right_center"></div>
</div>
<div class="div_clear">
<div class="left_bottom"></div>
<div class="center_bottom">
<span> 共 ${pagenum}页 当前 第${page}页</span>
<div style="float: right; padding-right: 30px">
<c:choose>
<c:when test="${page>1}">
<a href="Losty?page=${page-1}"><input type="button"
value="上一页"></a>
</c:when>
<c:otherwise>
<input type="button" value="上一页" disabled="disabled" />
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${page!=pagenum}">
<a href="Losty?page=${page+1}"><input type="button"
value="下一页"></a>
</c:when>
<c:otherwise>
<input type="button" value="下一页" disabled="disabled" />
</c:otherwise>
</c:choose>
<form action="Losty">
<input type="text" name="page" size="1" /> <input type="submit"
value="跳转" />
</form>
</div>
</div>
<div class="right_bottom"></div>
</div>
</div>
</body>
</html>
接下来是servlet进行处理
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import beans.lost;
import userdao.Lost;
import userdao.userdao;
@WebServlet("/Losty")
public class Losty extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String page = null;
String loginname=request.getParameter("loginname");
page = request.getParameter("page");
if(page == null || page=="")
page = "1";
Lost studao = new Lost();
request.setAttribute("student",studao.findByPage(Integer.parseInt(page)));
request.setAttribute("pagenum",studao.userCount()/5+1);//总页数
request.setAttribute("loginname", loginname);
request.setAttribute("page", page);//当前页
request.getRequestDispatcher("ip/index.jsp").forward(request, response);
}
}
数据库部分
public ArrayList<lost> findByPage(int page){
conn=getConnectionn();
int begin = (page-1) * 5;//每页数据量
String sql = "select * from lost limit "+begin+",5";
ResultSet rs = executeQuery(sql,null);
ArrayList<lost> list = new ArrayList<lost>();
try {
while(rs.next()){
lost st = new lost();
st.setBiaoti(rs.getString("biaoti"));
st.setMiaoshu(rs.getString("miaoshu"));
st.setAdress(rs.getString("adress"));
list.add(st);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{ close(); }
return list;
}
public int userCount(){
conn=getConnectionn();
String sql = "select count(*) from lost";//lost为表名
ResultSet rs =executeQuery(sql, null);
int count = 0;
try {
rs.next();
count = rs.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{ close(); }
return count;
}
}