package bean; import java.sql.*; import java.util.ArrayList; /** * Struts分頁顯示數(shù)據(jù)Bean,對應(yīng)數(shù)據(jù)庫中Book表 */ public class Book { PRivate String bookname; //書名 private String author; //作者 private String price; //價(jià)格
public Book(String name,String author,String price){ this.bookname=name; this.author=author; this.price=price; }
public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }
public String getBookname() { return bookname; }
public void setBookname(String bookname) { this.bookname = bookname; }
public String getPrice(){ return this.price; }
public void setPrice(String price){ this.price=price; }
public static ArrayList getAllBook(Connection connection){ String sql="select * from book"; ArrayList arrayList = new ArrayList(); try{ Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = statement.executeQuery(sql); System.out.println("BookBean 數(shù)據(jù)查詢已完成!"); while(resultSet.next()) { String name = resultSet.getString("name"); String author = resultSet.getString("author"); String price = resultSet.getString("price"); System.out.println("開始數(shù)據(jù)封裝:name="+name+"author="+author+"price="+price); Book book = new Book(name,author,price); arrayList.add(book); } connection.close(); resultSet.close(); }catch(SQLException e) { System.out.println("數(shù)據(jù)庫異常"+e.toString()); }
return arrayList; } }
2 PageBean.java package page; import bean.Book; import java.util.*; /** * Struts分頁顯示邏輯Bean */ public class PageBean {
int currentPage=1; //當(dāng)前頁 public int totalPages=0; //總頁數(shù) int pageRecorders=5;//每頁5條數(shù)據(jù) int totalRows=0; //總數(shù)據(jù)數(shù) int pageStartRow=0;//每頁的起始數(shù) int pageEndRow=0; //每頁顯示數(shù)據(jù)的終止數(shù) boolean hasNextPage=false; //是否有下一頁 boolean haspreviousPage=false; //是否有前一頁 ArrayList arrayList; Iterator it; public PageBean(){}