/**
*
*/
package s2jsp.ebook.dao;
import java.sql.*;
import java.util.*;
import s2jsp.ebook.entity.*;
/**
* @author Administrator
*
*/
public class Operator extends BaseDao{
private Connection conn=null;
private PreparedStatement pstmt=null;
private ResultSet rs=null;
private int allRows; //总共的数据的行数
private int curPage = 1;//当前页,默认第1页
private int rowPerPage = 5; //每页显示的数据的行数,5行
private int allPages; //总共的页数
/**
* 总共的数据的行数
* @return the allRows
*/
public int getAllRows() {
return allRows;
}
/**
* 总共的数据的行数
* @param allRows the allRows to set
*/
public void setAllRows(int allRows) {
this.allRows = allRows;
}
/**
* 当前页,默认第1页
* @return the curPage
*/
public int getCurPage() {
return curPage;
}
/**
* 当前页,默认第1页
* @param curPage the curPage to set
*/
public void setCurPage(int curPage) {
this.curPage = curPage;
}
/**
* 每页显示的数据的行数,5行
* @return the rowPerPage
*/
public int getRowPerPage() {
return rowPerPage;
}
/**
* 每页显示的数据的行数,5行
* @param rowPerPage the rowPerPage to set
*/
public void setRowPerPage(int rowPerPage) {
this.rowPerPage = rowPerPage;
}
/**
* 总共的页数
* @return the allPages
*/
public int getAllPages() {
return allPages;
}
/**
* 总共的页数
* @param allPages the allPages to set
*/
public void setAllPages(int allPages) {
this.allPages = allPages;
}
public List getBookList(){
List bookList = new ArrayList();
String sql = "select * from bookInfo order by bookid desc";
try {
conn=this.getConn();
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
rs.last(); //滚动到最后 游标移至最后一个记录的位置,如果ResultSet是空的,返回false
this.allRows=rs.getRow();//获得总共数据的行数 获得游标当前行的号码,从1开始
if(this.allRows%this.rowPerPage==0){//如果总共数据行数%每页5条数据页数正好
this.allPages=this.allRows/this.rowPerPage;//总共的页数=总共数据行数/每页5条数据
}else{
this.allPages=this.allRows/this.rowPerPage+1;//否则在加1页
}
if(this.curPage==1){
rs.first(); //如果当前页在第一页就将游标移动到第一页
//游标移至第一个记录的位置,如果ResultSet是空的,返回false
}
else{
rs.absolute((this.curPage-1)*this.rowPerPage); //如果不在第一页,将当前页减1乘以每页多少行
//若参数为正,游标移至第参数个记录的位置上,若为负,移至倒数第参数个记录的位置上,如果移动之后,游标指向的位置有记录,返回true.
}
int i = 0;//控制每次读5条rowPerPage默认每页5条
while(rs.next() && this.rowPerPage > i){
BookInfo book = new BookInfo();
book.setBookName(rs.getString("bookname"));
book.setBookType(rs.getString("booktype"));
book.setBookStatus(rs.getString("bookstatus"));
bookList.add(book);
i++;
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, rs);
}
return bookList;
}
}