当前位置:编程学习 > JAVA >>

分页的问题···

这个分页主要功能是一个用户已发过的帖子的分页,我定义了Page类,PageDaoI接口,PostPage类,以及两个数据操作。代码如下:
Page类:package page;

import java.sql.SQLException;
import java.util.List;

import valuebean.Poster;

public class Page<E> {
private int totalPage;
private int totalRecord;
private int recordPerPage=10;
private List<E> pageDate;
private int currentPage;

public Page(PageDaoI<E> pageDaoI, int currentPage,String userName)throws SQLException {
this.recordPerPage=recordPerPage;
this.totalRecord = pageDaoI.getTotalRecord(userName);
this.totalPage = totalRecord / recordPerPage;
if (this.totalPage * this.recordPerPage != this.totalRecord)
this.totalPage++;
if (currentPage < 1) {
this.currentPage = 1;
} else {
if (currentPage > this.totalPage) {
this.currentPage = this.totalPage;
} else {
this.currentPage = currentPage;
}
}
int min = (this.currentPage - 1) * recordPerPage + 1;
int max = min + recordPerPage - 1;
this.pageDate = pageDaoI.getPageDate(userName,min, max);
}

public int getTotalPage() {
return totalPage;
}
public int getTotalRecord() {
return totalRecord;
}
public int getRecordPerPage() {
return recordPerPage;
}
public List<E> getPageDate() {
return pageDate;
}
public int getCurrentPage() {
return currentPage;
}
}

PageDaoI接口:package page;

import java.sql.SQLException;
import java.util.List;

public interface PageDaoI<E> {
int getTotalRecord(String userName) throws SQLException ;

List<E> getPageDate(String userName,int min,int max) throws SQLException;

}


PostPage类:package dao;

import java.sql.SQLException;

import valuebean.Poster;

import page.Page;
import page.PageDaoI;


public class PostPage extends Page<Poster>{

public PostPage(PageDaoI<Poster> pageDaoI,int currentPage,String userName) throws SQLException {
super(pageDaoI,currentPage,userName);
}


}


两个数据操作: @Override
    public int getTotalRecord(String userName)throws SQLException{
     Connection conn = null;
PreparedStatement stat = null;
ResultSet rst = null;
Poster poster=new Poster();
int rowCount = 0;
try {
conn = DB.getConnection();
stat =  conn.prepareStatement("select count(*) from [Bbs_Post] where PostSender=?");
stat.setInt(1, rowCount);
stat.setString(2, userName);
while(rst.next()){
rowCount=rst.getInt(1);
}
} finally {
DB.release(conn, stat, rst);
}
return rowCount;
    }
    @Override
    public List<Poster> getPageDate(String userName,int min,int max) throws SQLException {
ArrayList<Poster> userList = new ArrayList<Poster>();
Poster poster=new Poster();
Connection conn = null;
PreparedStatement stat = null;
ResultSet rst = null;
try {
conn = DB.getConnection();
stat = conn
.prepareStatement("select PostTitle,PostSendTime from (select PostTitle,PostSendTime,ROW_NUMBER() Over(order by PostId DESC) as rowNum from Bbs_Post where PostSender=?) as tmp where rowNum between ? and ?;");
stat.setString(1, userName);
stat.setInt(2, min);
stat.setInt(3, max);
rst = stat.executeQuery();
while (rst.next()) {
poster.setPostId(rst.getInt("PostId"));
poster.setPostTitle(rst.getString("PostTitle"));
poster.setPostContent(rst.getString("PostSendTime"));
userList.add(poster);
}
} finally {
DB.release(conn, stat, rst);
}
return userList;
}


报错貌似是说Invalid parameter binding(s) --------------------编程问答--------------------    你是不懂做分页吗?我博客有三大主流数据库的分页实现的SQL语句
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,