日期:2014-05-18  浏览次数:20718 次

怎样用java实现分页显示
我用hibernate   +   struts   写了一个数据库查询的系统,用java怎样才能实现分页显示?能给出例子更好!!!本人正在全力学习java,希望高手多多指点!!!本人联系方式:weokme@163.com     QQ:526881969   谢谢!

------解决方案--------------------

/**
* 根据最大页数、开始记录数返回对应记录集
* @param pageSize 最大页数
* @param page 开始记录数
* @return
* @throws Exception
*/
public List getRSofPage(int pageSize,int page) throws Exception {
List retList = new ArrayList();
Session sess = null;
try {
sess = HibernateUtil.currentSession();
Transaction tx = sess.beginTransaction();
Query q = sess
.createQuery( "from Channel where ParentId is not 0 order by ParentId ,ChannelId ");
q.setMaxResults( pageSize );
q.setFirstResult( (page - 1) * pageSize );
retList = q.list();
tx.commit();
//log
logger.info( "(@@@@@@@@@@@ 根据最大页数、开始记录数返回对应记录集执行正常 @@@@@@@@@@@) ");
} catch (HibernateException he) {
//log
logger.error( "(@@@@@@@@@@@ 根据最大页数、开始记录数返回对应记录集执行异常 @@@@@@@@@@@) ", he);
new org.hibernate.HibernateException( "getRSofPage(): "+listErrors);
} finally {
try {
HibernateUtil.closeSession();
} catch (HibernateException he) {
new org.hibernate.HibernateException(
"HibernateUtilServlet.closeSession() ");
}
}
return retList;
}
------解决方案--------------------
你要计算的,我给你个类似的例子,是我以前写的,也是struts + hibernate

存放信息的实体类
package com.entitys;

import java.io.Serializable;

public class Product implements Serializable{
private String productId;
private String productName;
private int num;
private double price;
private String factoryName;
private String tel;

public Product(){}

public Product(String productId,String productName,int num,double price,String factoryName,String tel){
this.productId = productId;
this.productName = productName;
this.num = num;
this.price = price;
this.factoryName = factoryName;
this.tel = tel;
}

public String getFactoryName() {
return factoryName;
}
public void setFactoryName(String factoryName) {
this.factoryName = factoryName;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}

访问数据库的类
package com.dbaccess;

import java.util.Collection;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.Icommand.IProduct;
import com.hibernateFactory.HibernateSessionFactory;

public class AccessProduct implements IProduct {