论坛首页 综合技术论坛

SQLSERVER2000分页代码

浏览 1712 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-11-10  
方法:selectOnePage_queryToHashTabl(String sql,int pageNo, int numPerPage)
参数说明:  String sql  ---要查询的sql
          int pageNo  ----要查询的页码
          int numPerPage  -----每页显示多少条记录
返回值:Vector.  本方法返回一个Vector,其中Vector中的每一个元素是一个Hashtable.每个Hashtable中存放着一行(条)记录。Vector的第一个元素放着分页信息(一共多少页,一共多少条记录)。顾只要从Vector的第二个元素开始循环即可获取所有的记录。
 
 
用java 写的
public Vector selectOnePage_queryToHashTabl(String sql,int pageNo, int numPerPage)throws Exception   {
      Vector list = new Vector();
       Connection con = null;
       Statement stmt = null;
       ResultSet rs = null;
       try
       {
           con = getConnection();
           stmt = con.createStatement();
           rs = stmt.executeQuery(sql);  //首先获取所有满足条件的记录条数
           ResultSetMetaData columns=rs.getMetaData();
           int columnCount=rs.getMetaData().getColumnCount();
           int rowNo = 1;
           int totalPages = 0;
           int totalRecords=0;
           while(rs.next())
             {
                 if(rowNo>(pageNo-1)*numPerPage && rowNo<=pageNo*numPerPage)
                 {
                  Hashtable table= new Hashtable();
                     try
                     {
                         for(int i=1;i<=columnCount;i++)
                         {
                          String columnValue = rs.getString(i);
                             String columnname=columns.getColumnName(i);
                             table.put(columnname,columnValue);   
                         }
                         list.add(table);
                     }
                     catch(Exception ex)
                     {
                         throw ex;
                     }
                 }
                 rowNo++;
                 totalRecords++;
             }
             if(totalRecords % numPerPage == 0)
             {
                 totalPages = totalRecords / numPerPage;
             }
             else
             {
                 totalPages = totalRecords / numPerPage + 1;
             }
            
             Hashtable table= new Hashtable();             
            
             table.put("totalPages",Integer.toString(totalPages));//共有几页
                         
             table.put("totalRecords",Integer.toString(totalRecords));//共有几条记录            
            
             list.insertElementAt(table,0);//第一条记录放置分页信息
           
       }
       catch(Exception ex)
       {
           throw ex;
       }
       finally
       {
           try
           {
               rs.close();
               stmt.close();
               con.close();
           }
           catch(Exception ex)
           {
               throw ex;
           }
       }
       if(list.size() == 0)
       {
           return null;
       }
       else
       {
           return list;
       }
   }
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics