下文為您介紹DB2分頁的腳本,供您參考學習,如果您對DB2分頁方面感興趣的話,不妨一看,相信對您學習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;
以上就是DB2分頁的實現方法。