求java web 利用结果集进行分页代码 servlet+jsp
2个回答
展开全部
以下是最简单的分页(一个方法搞定):
首先是方法:
//用于按照页数和每页显示的记录数进行图书查询
public List getBookList(int page,int pageSize)throws Exception{
List list=new ArrayList();
Connection conn=this.getConn();
String sql="select top "+page+" * from bookInfo " +
"where bookId not in" +
"(select top "+((pageSize-1)*page)+" bookId from bookInfo)";
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
//每条数据对应一个实体类
BookInfo book=new BookInfo();
book.setBookId(rs.getInt("bookId"));
book.setBookName(rs.getString("bookName"));
book.setBookType(rs.getString("bookType"));
book.setBookStatus(rs.getString("bookStatus"));
list.add(book);
}
this.closeAll(conn, pstmt, rs);
return list;
}
2:JSP页面代码:
<%@ page language="java" import="java.util.*,entity.*,operator.*" pageEncoding="gbk"%>
<html>
<head>
</head>
<body>
<table border="1">
<tr>
<td width="150">图书名称</td>
<td width="100">类别</td>
<td width="100">状态</td>
</tr>
<%
request.setCharacterEncoding("gbk");
int pageNum=1;
String pageStr=request.getParameter("pageNum");
if(pageStr==null){
pageNum=1;
}else{
pageNum=Integer.parseInt(pageStr);
}
Operator o=new Operator();
List list=o.getBookList(6,pageNum);
for(int i=0;i<list.size();i++){
BookInfo book=(BookInfo)list.get(i);
%>
<tr>
<td><%=book.getBookName() %></td>
<td><%=book.getBookType() %></td>
<td><%=book.getBookStatus() %></td>
</tr>
<%
}
%>
<tr>
<td colspan="3"><a href="infoList.jsp?pageNum=<%=pageNum==1?pageNum:pageNum-1%>">上一页</a> ||
<a href="infoList.jsp?pageNum=<%=list.size()<6?pageNum:pageNum+1 %>">下一页</a></td>//二元运算符,相当于if判断 上一页时如果当前页=1为true,就是当前页,否则还可以减1
//下一页如果当前页的size小于你的每一页设置的条数为true,那么就是是当前页,否则就+1;
</tr>
</table>
</body>
</html>
首先是方法:
//用于按照页数和每页显示的记录数进行图书查询
public List getBookList(int page,int pageSize)throws Exception{
List list=new ArrayList();
Connection conn=this.getConn();
String sql="select top "+page+" * from bookInfo " +
"where bookId not in" +
"(select top "+((pageSize-1)*page)+" bookId from bookInfo)";
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
//每条数据对应一个实体类
BookInfo book=new BookInfo();
book.setBookId(rs.getInt("bookId"));
book.setBookName(rs.getString("bookName"));
book.setBookType(rs.getString("bookType"));
book.setBookStatus(rs.getString("bookStatus"));
list.add(book);
}
this.closeAll(conn, pstmt, rs);
return list;
}
2:JSP页面代码:
<%@ page language="java" import="java.util.*,entity.*,operator.*" pageEncoding="gbk"%>
<html>
<head>
</head>
<body>
<table border="1">
<tr>
<td width="150">图书名称</td>
<td width="100">类别</td>
<td width="100">状态</td>
</tr>
<%
request.setCharacterEncoding("gbk");
int pageNum=1;
String pageStr=request.getParameter("pageNum");
if(pageStr==null){
pageNum=1;
}else{
pageNum=Integer.parseInt(pageStr);
}
Operator o=new Operator();
List list=o.getBookList(6,pageNum);
for(int i=0;i<list.size();i++){
BookInfo book=(BookInfo)list.get(i);
%>
<tr>
<td><%=book.getBookName() %></td>
<td><%=book.getBookType() %></td>
<td><%=book.getBookStatus() %></td>
</tr>
<%
}
%>
<tr>
<td colspan="3"><a href="infoList.jsp?pageNum=<%=pageNum==1?pageNum:pageNum-1%>">上一页</a> ||
<a href="infoList.jsp?pageNum=<%=list.size()<6?pageNum:pageNum+1 %>">下一页</a></td>//二元运算符,相当于if判断 上一页时如果当前页=1为true,就是当前页,否则还可以减1
//下一页如果当前页的size小于你的每一页设置的条数为true,那么就是是当前页,否则就+1;
</tr>
</table>
</body>
</html>
展开全部
package com.work.action;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tool.DBToolSinglecon;
import com.work.bean.*;
/**
*
* 查看分类
*
*/
@SuppressWarnings("serial")
public class SortShow extends HttpServlet {
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
// 总记录
int countSize = 0;
// 每页记录
int pageSize = 10;
// 总页数
int pageCount = 0;
// 当前页
int currentPage = 0;
// 当前页的起始记录
int currentPageBegin = 0;
// 当前记录集合
List<sortbean> list = new ArrayList<sortbean>();
// 初始化javabean对象
sortbean use = null;
String sql = "SELECT COUNT(*) FROM sorts;";
// 启用工具类DBToolSinglecon
DBToolSinglecon res = DBToolSinglecon.getInstance();
//调用工具类的查询方法
ResultSet rs = res.query(sql);
// 获得总记录
try {
if (rs.next()) {
countSize = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
// 获得总页数
pageCount = (countSize + pageSize - 1) / pageSize;
// 得到当前页数
String pages = request.getParameter("page");
String sqls = null;
//进行一系列的判断,定义相关的sql语句
if (pages == null || pages.equals("")) {
currentPage = 1;
sqls = "SELECT * FROM sorts limit 0," + pageSize + "";
} else {
currentPage = Integer.valueOf(pages);
if (currentPage <= 0) {
currentPage = 1;
currentPageBegin = (currentPage - 1) * pageSize;
sqls = "SELECT * FROM sorts ORDER BY sort_id limit "
+ currentPageBegin + "," + pageSize + "";
} else {
if (currentPage > pageCount) {
currentPage = pageCount;
currentPageBegin = (currentPage - 1) * pageSize;
sqls = "SELECT * FROM sorts ORDER BY sort_id limit "
+ currentPageBegin + "," + pageSize + ";";
} else {
currentPageBegin = (currentPage - 1) * pageSize;
sqls = "SELECT * FROM sorts ORDER BY sort_id limit "
+ currentPageBegin + "," + pageSize + ";";
}
}
}
//执行sql语句
ResultSet rss = res.query(sqls);
//把所有的分类塞进javaBean,然后存入list集合
try {
while (rss.next()) {
use = new sortbean();
use.setSort_id(Integer.valueOf(rss.getString("sort_id")));
use.setSort_name(rss.getString("sort_name"));
list.add(use);
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 把当前的页数传到下一个页面
request.setAttribute("currentpage", currentPage);
// 把总页数传到下一个页面
request.setAttribute("pagecount", pageCount);
// 把list对象传到下一个页面
request.setAttribute("list", list);
request.getRequestDispatcher("/AdminAction/querysort.jsp").forward(
request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
}
}
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>显示页面</title>
</head>
<body background="C:\Documents and Settings\Administrator\桌面\1.jpg">`
<a href="<%=path%>/AdminAction/AdminAction.jsp"><font color="#0000FF">返回主页面</font>
<%--得到list数据集合的对象 --%>
<%
Object list = request.getAttribute("list");
Object pagecount = request.getAttribute("pagecount");
Object currentpage = request.getAttribute("currentpage");
String li = (String) session.getAttribute("error");
%>
<center>
<%
if (null != li) {
out.print("<font size=1 color='ff0000'>" + li + "</font>"
+ "<br><br>");
}
%>
<%--使用foreach把DB里的数据显示出来 --%>
<table border=1>
<tr>
<th bgcolor="#808080">
分类号
</th>
<th bgcolor="#808080">
分类名
</th>
<th bgcolor="#808080">
管理
</th>
</tr>
<c:forEach var="list" items="${list}">
<tr>
<td>
<c:out value="${list.sort_id}" escapeXml="false" />
</td>
<td>
<c:out value="${list.sort_name}" escapeXml="false" />
</td>
<td>
<a href="<%=basePath%>deletesort?id=${list.sort_id}"><font
color="#0000FF">删除</font> <br> </a>
</td>
</tr>
</c:forEach>
</table>
第<%=currentpage%>页 共<%=pagecount%>页
<a href="<%=path%>/pageshow?page=1">1</a>
<a href="<%=path%>/pageshow?page=2">2</a>
<a href="<%=path%>/pageshow?page=3">3</a>
<form action="<%=path%>/sortshow">
<input type="text" name="page" style="width: 20" />
<input type="submit" value="GO" />
</form>
</center>
</body>
</html>
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tool.DBToolSinglecon;
import com.work.bean.*;
/**
*
* 查看分类
*
*/
@SuppressWarnings("serial")
public class SortShow extends HttpServlet {
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
// 总记录
int countSize = 0;
// 每页记录
int pageSize = 10;
// 总页数
int pageCount = 0;
// 当前页
int currentPage = 0;
// 当前页的起始记录
int currentPageBegin = 0;
// 当前记录集合
List<sortbean> list = new ArrayList<sortbean>();
// 初始化javabean对象
sortbean use = null;
String sql = "SELECT COUNT(*) FROM sorts;";
// 启用工具类DBToolSinglecon
DBToolSinglecon res = DBToolSinglecon.getInstance();
//调用工具类的查询方法
ResultSet rs = res.query(sql);
// 获得总记录
try {
if (rs.next()) {
countSize = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
// 获得总页数
pageCount = (countSize + pageSize - 1) / pageSize;
// 得到当前页数
String pages = request.getParameter("page");
String sqls = null;
//进行一系列的判断,定义相关的sql语句
if (pages == null || pages.equals("")) {
currentPage = 1;
sqls = "SELECT * FROM sorts limit 0," + pageSize + "";
} else {
currentPage = Integer.valueOf(pages);
if (currentPage <= 0) {
currentPage = 1;
currentPageBegin = (currentPage - 1) * pageSize;
sqls = "SELECT * FROM sorts ORDER BY sort_id limit "
+ currentPageBegin + "," + pageSize + "";
} else {
if (currentPage > pageCount) {
currentPage = pageCount;
currentPageBegin = (currentPage - 1) * pageSize;
sqls = "SELECT * FROM sorts ORDER BY sort_id limit "
+ currentPageBegin + "," + pageSize + ";";
} else {
currentPageBegin = (currentPage - 1) * pageSize;
sqls = "SELECT * FROM sorts ORDER BY sort_id limit "
+ currentPageBegin + "," + pageSize + ";";
}
}
}
//执行sql语句
ResultSet rss = res.query(sqls);
//把所有的分类塞进javaBean,然后存入list集合
try {
while (rss.next()) {
use = new sortbean();
use.setSort_id(Integer.valueOf(rss.getString("sort_id")));
use.setSort_name(rss.getString("sort_name"));
list.add(use);
}
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 把当前的页数传到下一个页面
request.setAttribute("currentpage", currentPage);
// 把总页数传到下一个页面
request.setAttribute("pagecount", pageCount);
// 把list对象传到下一个页面
request.setAttribute("list", list);
request.getRequestDispatcher("/AdminAction/querysort.jsp").forward(
request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
}
}
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>显示页面</title>
</head>
<body background="C:\Documents and Settings\Administrator\桌面\1.jpg">`
<a href="<%=path%>/AdminAction/AdminAction.jsp"><font color="#0000FF">返回主页面</font>
<%--得到list数据集合的对象 --%>
<%
Object list = request.getAttribute("list");
Object pagecount = request.getAttribute("pagecount");
Object currentpage = request.getAttribute("currentpage");
String li = (String) session.getAttribute("error");
%>
<center>
<%
if (null != li) {
out.print("<font size=1 color='ff0000'>" + li + "</font>"
+ "<br><br>");
}
%>
<%--使用foreach把DB里的数据显示出来 --%>
<table border=1>
<tr>
<th bgcolor="#808080">
分类号
</th>
<th bgcolor="#808080">
分类名
</th>
<th bgcolor="#808080">
管理
</th>
</tr>
<c:forEach var="list" items="${list}">
<tr>
<td>
<c:out value="${list.sort_id}" escapeXml="false" />
</td>
<td>
<c:out value="${list.sort_name}" escapeXml="false" />
</td>
<td>
<a href="<%=basePath%>deletesort?id=${list.sort_id}"><font
color="#0000FF">删除</font> <br> </a>
</td>
</tr>
</c:forEach>
</table>
第<%=currentpage%>页 共<%=pagecount%>页
<a href="<%=path%>/pageshow?page=1">1</a>
<a href="<%=path%>/pageshow?page=2">2</a>
<a href="<%=path%>/pageshow?page=3">3</a>
<form action="<%=path%>/sortshow">
<input type="text" name="page" style="width: 20" />
<input type="submit" value="GO" />
</form>
</center>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询