实现的分页主要有两种,一种是客户端分页(假分页),另一种是服务器端分页,所谓客户端分页就是用一条SQL语句select * from table,将所有记录都取出来,然后再客户端知道了是第几页后用rs.absulte(pagesize*currentPage)定位到某一条记录,从而实现分页。服务器端分页是指将要显示的页码发送到服务器端,服务器端计算取出的记录是从哪一条到哪一条,然后构造特定的SQL语句,譬如MYSQL可以用LIMIT实现从哪一条记录到哪一条记录,ACCESS和MSSQL可以用TOP实现去除前多少条,然后再嵌套一个TOP实现取出特定条数的记录.一般采用服务器端分页,因为客户端分页每次都得查询整个数据库,效率较低。
//我会存储过程、 和对list进行分页、
实现的分页主要有两种,一种是客户端分页(假分页),另一种是服务器端分页,所谓客户端分页就是用一条SQL语句select * from table,将所有记录都取出来,然后再客户端知道了是第几页后用rs.absulte(pagesize*currentPage)定位到某一条记录,从而实现分页。服务器端分页是指将要显示的页码发送到服务器端,服务器端计算取出的记录是从哪一条到哪一条,然后构造特定的SQL语句,譬如MYSQL可以用LIMIT实现从哪一条记录到哪一条记录,ACCESS和MSSQL可以用TOP实现去除前多少条,然后再嵌套一个TOP实现取出特定条数的记录.一般采用服务器端分页,因为客户端分页每次都得查询整个数据库,效率较低。顶
实现的分页主要有两种,一种是客户端分页(假分页),另一种是服务器端分页,所谓客户端分页就是用一条SQL语句select * from table,将所有记录都取出来,然后再客户端知道了是第几页后用rs.absulte(pagesize*currentPage)定位到某一条记录,从而实现分页。服务器端分页是指将要显示的页码发送到服务器端,服务器端计算取出的记录是从哪一条到哪一条,然后构造特定的SQL语句,譬如MYSQL可以用LIMIT实现从哪一条记录到哪一条记录,ACCESS和MSSQL可以用TOP实现去除前多少条,然后再嵌套一个TOP实现取出特定条数的记录.一般采用服务器端分页,因为客户端分页每次都得查询整个数据库,效率较低。顶
用自定交标签 写 至于数据库 楼上写的都不错 1,写java 文件 public class PageJstl extends SimpleTagSupport { private PageBean myPageBean; private String queryType; public PageBean getMyPageBean() { return myPageBean; } public void setMyPageBean(PageBean myPageBean) { this.myPageBean = myPageBean; } public void doTag() throws JspException, IOException { // TODO Auto-generated method stub JspWriter out = this.getJspContext().getOut();
就是html写出来就好了
代码弄个Page类,每次查询(页数-1)*每页条数+1到页数*每页条数个
呵呵,楼主说的效果,正是我前段时间共享到 csdn 资源的 JavaBean 分页源码,楼主可以去看看,下载后即可使用,注释的相当详细
JavaBean 分页源码是很大众化的分页方案
/** *//**
* 通用 DAO 包
* @author kiant
* @version Sep 7, 2008
*/
public class CommomsDAO {
private static final Log log = LogFactory.getLog(EcOpusDAO.class);
//获得会话
private static HibernateTemplate hibernateTemplate = new HibernateTemplate(HibernateSessionFactory.getSessionFactory());
/** *//**
* 分页通用方法
* @param hql HQL查询语句
* @param offset 起始记录下标
* @param lengh 读取记录数
* @return List 结果集
*/
public static List getListForPage(final String hql, final int offset, final int lengh) {
log.debug("finding ListForPage");
try {
List list = hibernateTemplate.executeFind(new HibernateCallback(){ public Object doInHibernate(Session session)
throws HibernateException, SQLException {
List list2 = session.createQuery(hql)
.setFirstResult(offset)
.setMaxResults(lengh)
.list();
return list2;
}});
return list;
} catch (RuntimeException re) {
log.error("find ListForPage failed", re);
throw re;
}
}
}
自己还可以写个page类,用来做分页。
限制取出条数的SQL语法因数据库不同而不同,Hibernate只是帮你选择了正确的数据库方言而已(例如limit/top等SQL方言)取出来的就是页面一页要显示的,翻页的话,原理就是先在数据库连上之后把游标移动到何时的位置,再从游标处取下一页的数据
/WEB-INF/classes/applicationContext.xml分页的JSP通用页面pageman
<%@ 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>
<script language="javaScript">
<!--
function Jumping()
{
document.PageForm.submit();
}function gotoPage(pagenum)
{
document.PageForm.jumpPage.value=pagenum;
document.PageForm.submit();
}//-->
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here </title>
</head>
<body>
每页 <%=pageCtl.getRowsPerPage()%>行
共 <%=pageCtl.getMaxRowCount()%>行
第 <%=pageCtl.getCurPage()%>页
共 <%=pageCtl.getMaxPage()%>页
<br> <%if(pageCtl.getCurPage()==1){out.print("首页 上一页");} else{ %>
<A href="javascript:gotoPage(1)">首页 </A>
<A href="javascript:gotoPage( <%=pageCtl.getCurPage()-1%>)">上一页 </A>
<%} %>
<%if(pageCtl.getCurPage()==pageCtl.getMaxPage()){out.print("下一页 尾页");} else{ %>
<A href="javascript:gotoPage( <%=pageCtl.getCurPage()+1%>)">下一页 </A>
<A href="javascript:gotoPage( <%=pageCtl.getMaxPage()%>)">尾页 </A>
<%} %> 转到第 <select name="jumpPage" onchange="Jumping()">
<%
for(int i=1;i <=pageCtl.getMaxPage();i++)
{
if(i==pageCtl.getCurPage()){
%>
<option selected value= <%=i%>> <%=i%> </option>
<%}else{ %>
<option value= <%=i%>> <%=i%> </option>
<% }
}
%>
</select>页</body>
</html>分页主页面
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<jsp:useBean id="pageCtl" class="page.PageBean" scope="request"> </jsp:useBean>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><%@page import="java.util.Vector"%>
<%@page import="java.util.Enumeration"%> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here </title>
</head>
<body>
<table border=1>
<%
java.util.Vector v=pageCtl.getResult();
java.util.Enumeration e = v.elements();
while(e.hasMoreElements())
{
Object[] obj = (Object[])e.nextElement();
%>
<tr>
<td align="center" width="50%"> <%=obj[0]%> </td>
<td align="center" width="50%"> <%=obj[1]%> </td>
</tr>
<%} %>
</table><%if(pageCtl.getMaxPage()!=1){ %><form id="PageForm" name="PageForm" action="/LMSTEST/ContactServlet" method="get">
<%@ include file="/pageman.jsp" %> </form>
<%} %>
</body>
</html>分页程序ContactServlet.java
import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import page.ContactBean;
import page.PageBean;/**
* Servlet implementation class ContactServlet
*/
public class ContactServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* Default constructor.
*/
public ContactServlet() {
// TODO Auto-generated constructor stub
}/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
try {
ContactBean contact = new ContactBean();
PageBean pageCtl = contact.listData((String) request
.getParameter("jumpPage"));
System.out.print(pageCtl.getMaxPage());
request.setAttribute("pageCtl", pageCtl);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
RequestDispatcher dis = request.getRequestDispatcher("/contact.jsp");
dis.forward(request, response);
}/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}}ContactBean.java
package page;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;import com.neusoft.njpro.Test;public class ContactBean {
private Connection conn;
Vector v;
/**
* 创建数据库一个连接
* 初始化一个vector
*/
public ContactBean()
{
Test test = new Test();
conn = test.getCon();
v = new Vector();
}/**
* @return 要查询的记录数
* @throws SQLException
*/
public int getAvailableCount() throws SQLException
{
int ret = 0;
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("select count(*) from procedure_test");
while(rset.next())
{
ret = rset.getInt(1);
}return ret;
}/**
* @param page获取指定页面的数据,并且封装在PageBean中返回
* @return
* @throws Exception
*/
public PageBean listData(String page) throws Exception
{
try {
PageBean pageBean = new PageBean(this);
int pageNum = Integer.parseInt(page);
Statement stmt = conn.createStatement();
String strSql = "select * from (select rownumber() over(order by log_time) as rn,procedure_test.* from procedure_test) as t where t.rn between "
+ (pageNum * pageBean.rowsPerPage-pageBean.rowsPerPage+1)
+ " and "+ pageNum * pageBean.rowsPerPage;
ResultSet rset = stmt.executeQuery(strSql);while (rset.next()) {Object[] obj = new Object[2];
obj[0] = rset.getString(3);
obj[1] = rset.getObject(4);
v.add(obj);}
rset.close();
stmt.close();
pageBean.curPage = pageNum;
pageBean.data = v;
return pageBean;
} catch (Exception e) {
e.printStackTrace();
throw e;
// TODO: handle exception
}
}public Vector getResult()
{
return v;
}}PageBean.java
package page;import java.sql.SQLException;
import java.util.Vector;public class PageBean {
public int getCurPage() {
return curPage;
}public void setCurPage(int curPage) {
this.curPage = curPage;
}public int getMaxPage() {
return maxPage;
}public void setMaxPage(int maxPage) {
this.maxPage = maxPage;
}public int getMaxRowCount() {
return maxRowCount;
}public void setMaxRowCount(int maxRowCount) {
this.maxRowCount = maxRowCount;
}public int getRowsPerPage() {
return rowsPerPage;
}public void setRowsPerPage(int rowsPerPage) {
this.rowsPerPage = rowsPerPage;
}public java.util.Vector getData() {
return data;
}public void setData(java.util.Vector data) {
this.data = data;
}public int curPage; //当前是第几页
public int maxPage; //一共多少页
public int maxRowCount; //一共是多少行
public int rowsPerPage = 5; //每页多少行
public java.util.Vector data; //本页中的资料public PageBean()
{}public void countMaxPage() //根据总行数计算总页数
{
if(this.maxRowCount%this.rowsPerPage==0)
{
this.maxPage = this.maxRowCount/this.rowsPerPage;
}
else
{
this.maxPage = this.maxRowCount/this.rowsPerPage+1;
}
}public Vector getResult()
{
return this.data;
}public PageBean(ContactBean contact) throws SQLException
{
this.maxRowCount = contact.getAvailableCount(); //得到总行数
this.data = contact.getResult(); //得到要显示于本页的资料
this.countMaxPage();
}
}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<html>
<head>
<script type="text/javascript">
function goto(){
var page=document.getElementById("pp").value;var url=null;
if(page==""||page==null){
alert("不能为空");
return;
}else{
url="PageServlet?page="+page;
}location.href=url;
}
</script>
<title>分页</title><link rel="stylesheet" href="css/css.css" type="text/css"></link></head><body>
<form action="PageServlet" name="pageForm">
<table>
<c:forEach items="${requestScope.list}" var="page">
<c:if test="${page!=null}">
<tr>
<td>${page.num }</td>
</tr>
</c:if>
</c:forEach>
</table>
</from>
<a href="PageServlet?page=1">首页</a> 总记录数/总页数 ${requestScope.sumCount}/${requestScope.sumPage} 您现在位于第${requestScope.page }页
每页显示 5 条记录
<a href="PageServlet?page=1">[1]</a>
<c:if test="${requestScope.page!=1}">
<c:choose>
<c:when test="${requestScope.page<=5}">
<c:forEach var="i" begin="2" end="${requestScope.page}">
<a href="PageServlet?page=${i}"> [${i }]</a>
</c:forEach>
</c:when>
<c:otherwise>
...
<c:forEach var="i" begin="${requestScope.page-3}"
end="${requestScope.page}">
<a href="PageServlet?page=${i}">[${i}]</a>
</c:forEach>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${requestScope.page>=requestScope.sumPage-4
|| requestScope.sumPage-4<=0}">
<c:forEach var="i" begin="${requestScope.page+1}"
end="${requestScope.sumPage}">
<a href="PageServlet?page=${i}">[${i }]</a>
</c:forEach>
</c:when>
<c:otherwise>
<c:forEach var="i" begin="${requestScope.page+1}"
end="${requestScope.page+3}">
<a href="PageServlet?page=${i}">[${i }]</a>
</c:forEach>
...
<a href="PageServlet?page=${requestScope.sumPage}">
[${requestScope.sumPage}]</a>
</c:otherwise>
</c:choose>
</c:if>
<a href="PageServlet?page=${requestScope.page-1 }">上一页</a>
<a href="PageServlet?page=${requestScope.page+1 }">下一页</a>
<a href="PageServlet?page=${requestScope.sumPage }">尾页</a><input type="button" name="Submit" value="转到" onClick="javascript:goto()"><input type="text" id="pp" name="pp" size=4/>页</body>
</html>
oracle 用> ,<
sqlServer 用top
至于数据库 楼上写的都不错
1,写java 文件
public class PageJstl extends SimpleTagSupport { private PageBean myPageBean;
private String queryType; public PageBean getMyPageBean() {
return myPageBean;
} public void setMyPageBean(PageBean myPageBean) {
this.myPageBean = myPageBean;
} public void doTag() throws JspException, IOException {
// TODO Auto-generated method stub
JspWriter out = this.getJspContext().getOut();
out.print("<TABLE cellSpacing=1> <TBODY> <TR>");
out.print("<TD><a href="+queryType+".do?type="+myPageBean.getType()+"><IMG src=\"file/2arrow_l_off.gif\" border=0></a></TD>");
if(myPageBean.getPagecode()>1)
out.print("<TD><a href="+queryType+".do?type="+myPageBean.getType()+"&pagecode="+(myPageBean.getPagecode()-1)+"><IMG src=\"file/1arrow_l_off.gif\" border=0></a></TD>");
else
out.print("<TD><IMG src=\"file/1arrow_l_off.gif\" border=0></TD>");
if(myPageBean.getPagecode()<myPageBean.getAllpage())
out.print("<TD><a href="+queryType+".do?type="+myPageBean.getType()+"&pagecode="+(myPageBean.getPagecode()+1)+"><IMG src=\"file/1arrow_r_off.gif\" border=0></a></TD>");
else
out.print("<TD><IMG src=\"file/1arrow_r_off.gif\" border=0></TD>");
out.print("<TD><a href="+queryType+".do?type="+myPageBean.getType()+"&pagecode="+myPageBean.getAllpage()+"><IMG src=\"file/2arrow_r_off.gif\" border=0></a></TD>");
out.print("</TR></TBODY></TABLE>");
out.flush();
} public String getQueryType() {
return queryType;
} public void setQueryType(String queryType) {
this.queryType = queryType;
}}
2, .tld 文件
<?xml version="1.0" encoding="UTF-8" ?><taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0"> <display-name>JSTL page</display-name>
<tlib-version>1.0</tlib-version>
<short-name>Fpage</short-name>
<uri>http://qingruxu.net/fpage</uri>
<tag>
<name>mypage</name>
<tag-class>jstl.PageJstl</tag-class>
<body-content>empty</body-content>
<attribute>
<name>myPageBean</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>service.PageBean</type>
</attribute>
<attribute>
<name>queryType</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute> </tag>
</taglib>
3,在页面上调用
先引入 <%@ taglib prefix="Fpage" uri="http://qingruxu.net/fpage" %>
<Fpage:mypage myPageBean="${pageBean}" queryType="chenshanAction"/>
${pageBean} 为数据集合 我放的是一个java类如下
public class PageBean { private int pagesize;//页大小
private int pagecode;//当前页
private int allcount;//所有记录数
private int allpage;//总页数
private int type;//type 值
private String reValue;//标题
private List data=new ArrayList();
可以用QUERY进行分页啊
用它的setFirstresult(0)表示从第0条数据开始分页
然后再用它的setMaxresult(10)表示每次取出最大的数据条数是10
当你获得当前页,做一个简单的计算就能正确的分页了啊
给你提点醒
0-------1----------9
10------2----------19
20------3-----------29
明白了吧
起始位置的算法(页数-1)*10
因为你每次都取出相同的条数你下次起始位置不就是(页数*10)-1了嘛
对吧
Java海量数据分页Bean, 适用于Oracle(适当修改,适用于任何数据库).功能描述:传入到达页码(具有容错性)、每页记录数、Select查询语句,返回该页所有的记录(整页是List集合,每条记录是一个 HashMap)、总行数、总页数、开始行数、结束行数。 注:此为3.0版,有清晰的注释,你可以看到笔者怎样将其一步步优化为适用于海量数据的分布Bean。目前用于20万条数据记录的分页,健步如飞,未见任何异常。 下载地址:http://download.csdn.net/source/886528