package bean; import java.sql.*; import java.util.ArrayList; /** * Struts分頁顯示數據Bean,對應數據庫中Book表 */ public class Book { PRivate String bookname; //書名 private String author; //作者 private String price; //價格
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 數據查詢已完成!"); while(resultSet.next()) { String name = resultSet.getString("name"); String author = resultSet.getString("author"); String price = resultSet.getString("price"); System.out.println("開始數據封裝: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("數據庫異常"+e.toString()); }
return arrayList; } }
2 PageBean.java package page; import bean.Book; import java.util.*; /** * Struts分頁顯示邏輯Bean */ public class PageBean {
int currentPage=1; //當前頁 public int totalPages=0; //總頁數 int pageRecorders=5;//每頁5條數據 int totalRows=0; //總數據數 int pageStartRow=0;//每頁的起始數 int pageEndRow=0; //每頁顯示數據的終止數 boolean hasNextPage=false; //是否有下一頁 boolean haspreviousPage=false; //是否有前一頁 ArrayList arrayList; Iterator it; public PageBean(){}