資料庫記錄分頁顯示的類

來源:互聯網
上載者:User

忙了一下午,寫了個分頁顯示的類,自我感覺寫的還不錯,所以就貼出來了,呵呵!!!

 不過還是有問題:每次翻頁操作都要查詢整個資料庫,如果資料庫很大的話,真不敢想象會發生什麼事情。如果誰有解決的方法,希望能拿出來分享一下,先謝了!!!!!!!!!!!!!!!

import java.io.*;
import java.sql.*;
import java.util.*;

public class GetPage...{
  private int pageSize = 6; //每一頁顯示的記錄數
  private int pageCount = 0;  //總的頁數
  private int currentPage = 1;  //當前顯示的頁
  //"showVar" 值為"1"表示顯示第一頁,為"2"表示顯示上一頁,為"3"表示顯示下一頁,為"4"表示顯示最   後頁,否則顯示當前頁。
  public Vector<Vector> doGet(int showVar)...{
    Vector<Vector> all = new Vector<Vector>(); //當前頁的記錄集
    try ...{
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //定義資料庫驅動
      String url =
          "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=lesson"; //串連資料庫"lesson"
      String user = "sa";  //使用者名稱
      String password = "123";  //密碼
      Connection con = DriverManager.getConnection(url, user, password); //串連
      Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                                           ResultSet.CONCUR_UPDATABLE);
      String sql = "select * from upLoad";  //查詢語句
      ResultSet rs = stmt.executeQuery(sql);
      rs.last(); 
      int lastrow = rs.getRow(); //擷取記錄的總行數
      pageCount = (lastrow % pageSize == 0) ? (lastrow / pageSize) : (lastrow / pageSize + 1);
      if (pageCount == 0) ...{
        pageCount = 1;
      }
      switch (showVar) ...{
        case 1:
          currentPage = 1;
          break;
        case 2:
          currentPage = (currentPage > 1) ? (currentPage - 1) : 1;
          break;
        case 3:
          currentPage = (currentPage < pageCount) ? (currentPage + 1) : pageCount;
          break;
        case 4:
          currentPage = pageCount;
          break;
        default:
          currentPage = currentPage;
      }
      int posion = (currentPage - 1) * pageSize + 1;
      rs.absolute(posion);
      rs.previous(); //定位指標
      //****************擷取當前頁的資料*******************
      Vector<Integer> id = new Vector<Integer>();
      Vector<String> zhuti = new Vector<String>();
      for (int i = 0; i < pageSize && rs.next(); i++) ...{
        id.add(rs.getInt("id"));  
        zhuti.add(rs.getString("zhuti"));
      }
      all.add(id);
      all.add(zhuti);
      //*************************************************
      rs.close();
      stmt.close();
      con.close();
    }
    catch (Exception ex) ...{
      ex.printStackTrace();
    }
    return all;
  }
  public int getPageCount()...{
    return pageCount;
  }
  public int getCurrentPage()...{
    return currentPage;
  }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.