import java.util.*; import java.sql.*;public class PageBean { private int curPage=1;//当前是第几页 private int maxPage;//一共多少页 private int maxRowCount;//一共多少行 private int rowsPerPage=5;//每页多少行 Connection conn=null; public Vector data;
public PageBean() throws Exception{ this.setPageBean(); } public int getCurPage() { return curPage; } public int getMaxPage() { return maxPage; } public int getMaxRowCount() { return maxRowCount; } public void setCurPage(int curPage) { this.curPage = curPage; } public void setMaxPage(int maxPage) { this.maxPage = maxPage; } public void setMaxRowCount(int maxRowCount) { this.maxRowCount = maxRowCount; }
下面给你一个以前用的代码?现在都不用了,你看看适合你吗?
import java.sql.*;public class PageBean {
private int curPage=1;//当前是第几页
private int maxPage;//一共多少页
private int maxRowCount;//一共多少行
private int rowsPerPage=5;//每页多少行
Connection conn=null;
public Vector data;
public PageBean() throws Exception{
this.setPageBean();
} public int getCurPage() {
return curPage;
} public int getMaxPage() {
return maxPage;
} public int getMaxRowCount() {
return maxRowCount;
} public void setCurPage(int curPage) {
this.curPage = curPage;
} public void setMaxPage(int maxPage) {
this.maxPage = maxPage;
} public void setMaxRowCount(int maxRowCount) {
this.maxRowCount = maxRowCount;
}
public PageBean getResult(String page) throws Exception{
try{
String tableName=null;//数据库表名
PageBean pageBean=new PageBean();
Vector<Object> v=new Vector<Object>();
int pageNum=Integer.parseInt(page);
Statement stmt=conn.createStatement();
String strSql="select top"+pageNum*pageBean.rowsPerPage+"* from "+tableName;
ResultSet rset=stmt.executeQuery(strSql);
int i=0;
while(rset.next()){
if(i>(pageNum-1)*pageBean.rowsPerPage-1){
Object[] obj=new Object[3];
obj[0]=rset.getString(2);
obj[1]=rset.getString(3);
obj[2]=rset.getString(5);
v.add(obj);
}
i++;
}
rset.close();
stmt.close();
pageBean.setCurPage(pageNum);
pageBean.data=v;
return pageBean;
}catch(Exception e){
e.printStackTrace();
throw e;
}
}
public int getAvailableCount()throws Exception{
int ret=0;
DataBaseConn cBean=DataBaseConn.newInstance();
conn=cBean.getConnDB();
Statement stmt=conn.createStatement();
String sql="select * from topic";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
ret++;
}
return ret;
}
public void setPageBean() throws Exception{
//得到总行数
this.setMaxRowCount(this.getAvailableCount());
if(this.maxRowCount%this.rowsPerPage==0){
//根据总行数计算页数
this.maxPage=this.maxRowCount/this.rowsPerPage;
}else{
this.maxPage=this.maxRowCount/this.rowsPerPage+1;
}
}
}