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(){}