1.封装一个dao类.封装一个实体类PageBean
package com.pdips.page;
import java.util.ArrayList;
import java.util.List;
public class PageBean<E> {
private int pageNo;//当前页数
private int pageSize;//每一页面显示的条数
private int tolpage; //数据库里数据的总条数
private List<E> obj = new ArrayList<E>();
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTolpage() {
return tolpage;
}
public void setTolpage(int tolpage) {
this.tolpage = tolpage;
}
public List<E> getObj() {
return obj;
}
public void setObj(List<E> obj) {
this.obj = obj;
}
@Override
public String toString() {
return "PageBean [pageNo=" + pageNo + ", pageSize=" + pageSize
+ ", tolpage=" + tolpage + ",setObj =" + obj + "]";
}
}
2.在dao中写一个查询数据的方法,并带有两个参数(int pageNo,int pageSize)
package com.pdips.dao.impl;
import java.util.ArrayList;
import java.util.List;
import com.pdips.bean.DisaseTypeBean;
import com.pdips.bean.DrugBean;
import com.pdips.dao.IDrugDao;
import com.pdips.page.PageBean;
import com.pdips.util.DBUtil;
/**
* 药剂/器械 数据层
* @author zcan
* 2016-11-02
*/
public class DrugDaoImpl extends DBUtil implements IDrugDao {
/**
* 查询全部药剂器械
* @param pageNo 当前页数
* @param pageSize 每一页面显示的条数
* @return 返回PageBean
* @throws Exception
*/
@Override
public PageBean<DrugBean> getDrugs(int pageNo, int pageSize) throws Exception {
conn = this.open();
PageBean<DrugBean> page = new PageBean<DrugBean>();
List<DrugBean> drugs = new ArrayList<DrugBean>();
String sql = "select d.dru_id,d.dru_name,dis.typ_id,dis.typ_name,"
+ "d.dru_genre,d.dru_purpose"
+ " from drug_hickey d join disastertype dis "
+ "on d.typ_id = dis.typ_id limit " + (pageNo-1)*pageSize + "," + pageSize;
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
DrugBean drug = new DrugBean();
drug.setId(rs.getInt("d.dru_id"));
drug.setName(rs.getString("d.dru_name"));
drug.setTypes(rs.getString("d.dru_genre"));
drug.setPurpose(rs.getString("d.dru_purpose"));
DisaseTypeBean dis = new DisaseTypeBean();
dis.setId(rs.getInt("dis.typ_id"));
dis.setName(rs.getString("dis.typ_name"));
drug.setDisType(dis);
drugs.add(drug);
}
String sql2 = "select count(*) as num from drug_hickey";
rs = conn.prepareStatement(sql2).executeQuery();
int num = 0;
while(rs.next()){
num = rs.getInt("num");
}
//这里封装pageBean实体
int pageTol = (int) Math.ceil((double)num/pageSize);
page.setTolpage(pageTol);
page.setObj(drugs);
page.setPageNo(pageNo);
page.setPageSize(pageSize);
this.close();
return page;
}