public class BookDisplay { int maxRows=20; int rows; int pages; int currentPage=1; String condition=""; ArrayList list=new ArrayList(); Book book=new Book();
public BookDisplay() { }
public BookDisplay(int maxRows,String condition) { this.maxRows=maxRows; this.condition=condition; }
public ArrayList List(){ DBAccess db=new DBAccess(); db.createConn(); String sql=null; int temp=0; if((currentPage-1)!=0){ sql="set rowcount "+ maxRows*(currentPage-1) +" select book_id from book_info" +condition+" order by book_id set rowcount 0"; System.out.println(sql); db.query(sql); while(db.next()){ temp=Integer.parseInt(db.getValue("book_id")); } db.closeRs(); db.closeStm(); } String conditionTemp=condition.replaceFirst("where", "and"); sql="set rowcount " + maxRows + " select book_info.*,book_sort.sort_name from book_info,book_sort where book_info.sort_id=book_sort.sort_id and book_id>"+temp +conditionTemp+" order by book_id set rowcount 0"; db.query(sql); int i=0; //System.out.println(sql); while(db.next()) { book.setBookId(Integer.parseInt(db.getValue("book_id"))); book.setAuthor(db.getValue("author")); book.setBookSerial(db.getValue("book_serial")); book.setBookName(db.getValue("book_name")); book.setChargeFlag(db.getValue("charge_flag")); book.setISBN(db.getValue("ISBN")); book.setLogoutFlag(db.getValue("logout_flag")); book.setPress(db.getValue("press")); //book.setPrice(Float.parseFloat(db.getValue("price"))); book.setSortName(db.getValue("sort_name")); book.setTranslator(db.getValue("translator")); list.add(book); // System.out.println("book:"+list.get(0).getBookId()); // System.out.println("serial:"+list.get(0).getBookSerial());
arraylist.add("11")这样测试一下
int maxRows=20;
int rows;
int pages;
int currentPage=1;
String condition="";
ArrayList list=new ArrayList();
Book book=new Book();
public BookDisplay() {
}
public BookDisplay(int maxRows,String condition) {
this.maxRows=maxRows;
this.condition=condition;
}
public ArrayList List(){
DBAccess db=new DBAccess();
db.createConn();
String sql=null;
int temp=0; if((currentPage-1)!=0){
sql="set rowcount "+ maxRows*(currentPage-1) +" select book_id from book_info" +condition+" order by book_id set rowcount 0";
System.out.println(sql);
db.query(sql);
while(db.next()){
temp=Integer.parseInt(db.getValue("book_id"));
}
db.closeRs();
db.closeStm();
} String conditionTemp=condition.replaceFirst("where", "and");
sql="set rowcount " + maxRows + " select book_info.*,book_sort.sort_name from book_info,book_sort where book_info.sort_id=book_sort.sort_id and book_id>"+temp +conditionTemp+" order by book_id set rowcount 0";
db.query(sql);
int i=0;
//System.out.println(sql);
while(db.next()) {
book.setBookId(Integer.parseInt(db.getValue("book_id")));
book.setAuthor(db.getValue("author"));
book.setBookSerial(db.getValue("book_serial"));
book.setBookName(db.getValue("book_name"));
book.setChargeFlag(db.getValue("charge_flag"));
book.setISBN(db.getValue("ISBN"));
book.setLogoutFlag(db.getValue("logout_flag"));
book.setPress(db.getValue("press"));
//book.setPrice(Float.parseFloat(db.getValue("price")));
book.setSortName(db.getValue("sort_name"));
book.setTranslator(db.getValue("translator"));
list.add(book);
// System.out.println("book:"+list.get(0).getBookId());
// System.out.println("serial:"+list.get(0).getBookSerial());
i++;
// list.add(i);
// System.out.println("i="+i);
}
// System.out.println("serial:"+list.get(0).getBookSerial());××××××××××××××××××这里打印出来就是第二个元素的值了
db.closeRs();
db.closeStm();
db.closeConn();
return list;
}
Book book=new Book();
.....
Book book=new Book(); ...
就是这样,你在外面新建的Book,在list里放的可不是就一个呗~你得每循环一次新建一个
应该把
Book book=new Book(); 放到循环里,这样list中加入的才会是不同的book对象
应该在循环开始的地方,每次都new一个新的才行
Book book=new Book();//创建对象放这里,不然 你添加N个 它的值都是一样的
book.setBookId(Integer.parseInt(db.getValue("book_id")));
book.setAuthor(db.getValue("author"));
book.setBookSerial(db.getValue("book_serial"));
book.setBookName(db.getValue("book_name"));
book.setChargeFlag(db.getValue("charge_flag"));
book.setISBN(db.getValue("ISBN"));
book.setLogoutFlag(db.getValue("logout_flag"));
book.setPress(db.getValue("press"));
//book.setPrice(Float.parseFloat(db.getValue("price")));
book.setSortName(db.getValue("sort_name"));
book.setTranslator(db.getValue("translator"));
list.add(book);
// System.out.println("book:"+list.get(0).getBookId());
// System.out.println("serial:"+list.get(0).getBookSerial());i++;
// list.add(i);
// System.out.println("i="+i);}