日期:2014-05-16  浏览次数:20565 次

db2分页的实现
String sqlcount="select count(*) from stu.book "+condition; 
    System.out.println(sqlcount);  
         int curPage; //当前需要显示的页码 
    int totalPages;   //总页数   
  int pageRecord=10; //每页要显示的记录条数 
    int totalRecords; //满足条件的总共的记录条数  
   if(CPage!=null ){     
curPage =Integer.parseInt(CPage);    
  if(curPage<1){       
curPage=1;    
  }     
}else{   
   curPage=1;   
  }    
Connection con=Dao.getConn();   //获取数据库连接  
   try {   
  ps=con.prepareStatement(sqlcount);   
  rs=ps.executeQuery(); 
    if(rs.next()){    
  totalRecords=rs.getInt(1);   
    if(totalRecords%pageRecord==0)  
      totalPages=totalRecords/pageRecord;
//当每页显示的记录条数能被总记录条数整除时 总页数为总记录条数除以每页显示的记录条数        
     else           
totalPages=totalRecords/pageRecord+1;
//当每页显示的记录条数不能被总记录条数整除时 总页数为总记录条数除以每页显示的记录条数的商再加1
      String sql;     
if(curPage==1){      
sql="select * from stu.book "+condition+" FETCH FIRST   "+ pageRecord+" ROWS ONLY";            
}else{      
sql="select * from stu.book "+condition+" and booknum not in ( select booknum from stu.book "+condition+" FETCH FIRST "+(curPage-1)*pageRecord+" ROWS ONLY )"+" FETCH FIRST   "+ pageRecord+" ROWS ONLY";       }   
   System.out.println(sql);      
ps=con.prepareStatement(sql);    
  rs=ps.executeQuery();  
    while(rs.next()){   
    Book b=new Book();     
  b.setBooknum(rs.getString(1));    
   b.setBookname(rs.getString(2));  
     b.setBookindate(rs.getString(3)); 
      b.setBorrower(rs.getString(4));  
     b.setLenddate(rs.getString(5));    
   b.setRemark(rs.getString(6));   
    al.add(b);              }
                      }else{  
    return al;      }    
} catch (SQLException e1) {  
        e1.printStackTrace(); 
   }     
return al;