欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Jsp真分頁實(shí)例---分頁

 更新時(shí)間:2017年04月11日 22:46:20   投稿:mdxy-dxy  
假分頁每次只取需要的數(shù)據(jù)返回給客戶端,比起真分頁沒有那么大的數(shù)據(jù)庫壓力。但也因?yàn)檫@個(gè)工作特性,所以假分頁的方法需要頻繁和服務(wù)器端進(jìn)行交互。既然頻繁交互,自然也會(huì)給服務(wù)器帶來負(fù)擔(dān)

網(wǎng)頁的分頁功能的實(shí)現(xiàn)比較簡單,實(shí)現(xiàn)方法也多種多樣。

今天總結(jié)一個(gè)簡單的Jsp真分頁實(shí)例。

首先,提到分頁就要先明確一個(gè)概念,何為真分頁何謂假分頁。

假分頁:一次性從數(shù)據(jù)庫讀出表的所有數(shù)據(jù)一次性的返回給客戶端,由js來控制每一頁的顯示。

真分頁:由程序控制,每一次只返回一頁大小的數(shù)據(jù),顯示到客戶端。

由此可以很清楚的分辨出真假分頁各自的優(yōu)缺點(diǎn):

假分頁:由于一次性讀出所有數(shù)據(jù)并返回給客戶端,如果數(shù)據(jù)量龐大,所以這一次的動(dòng)作可能是非常消耗服務(wù)器資源和帶寬的,

但是返回給客戶端以后就非常輕松了,客戶在一段時(shí)間內(nèi)不會(huì)再像服務(wù)器端請求資源。但不代表可能出現(xiàn)一些意外情況,

比如說客戶將瀏覽器關(guān)閉,重新訪問網(wǎng)站等。所以,如果數(shù)據(jù)量相當(dāng)龐大,不建議使用用真分頁。

真分頁:假分頁每次只取需要的數(shù)據(jù)返回給客戶端,比起真分頁沒有那么大的數(shù)據(jù)庫壓力。但也因?yàn)檫@個(gè)工作特性,所以假分頁

的方法需要頻繁和服務(wù)器端進(jìn)行交互。既然頻繁交互,自然也會(huì)給服務(wù)器帶來負(fù)擔(dān)。

綜上:如果數(shù)據(jù)量較小,使用假分頁的效果會(huì)更優(yōu),如果數(shù)據(jù)量龐大,使用真分頁的效果更優(yōu)。

分析完特性,下面就來列舉一個(gè)簡單的真分頁實(shí)例。

真分頁是通過程序來控制的,每次向數(shù)據(jù)庫請求需要的數(shù)據(jù)。

簡述實(shí)現(xiàn)思路業(yè)務(wù)流程:

首先:客戶端帶著page參數(shù)請求客戶端,若沒有帶page參數(shù),說明是第一次訪問,則page參數(shù)默認(rèn)為0;

其次:服務(wù)端根據(jù)page參數(shù),調(diào)用相關(guān)函數(shù),從數(shù)據(jù)庫中取出表中數(shù)據(jù),封裝成相關(guān)對象,返回給客戶端,并且返回新page參數(shù)及總頁數(shù);

最后:再客戶端顯示請求的相關(guān)數(shù)據(jù),并根據(jù)page參數(shù)及總頁數(shù)兩個(gè)參數(shù),決定上一頁下一頁的按鈕是否可用。

數(shù)據(jù)庫操作類:

public class DBBean {
  private Connection con;

  private PreparedStatement pstmt;
  private ResultSet rs;
  private String dbName ="test";
  private String dbuser = "root";
  private String dbpass ="******";
  
  static{
    try{
      Class.forName("com.mysql.jdbc.Driver");
    }catch(ClassNotFoundException e){
      System.out.println(e);
    }
    
  }
  
  public void prepareConnection(){
    try{
      con=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dbName,dbuser,dbpass);
    }catch(SQLException e){
      System.out.println(e);
    }
  }
  //關(guān)閉連接
  public void close(){
      try {
        if(con!=null)
          con.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      con = null;
      try {
        if(pstmt!=null)
          pstmt.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      pstmt = null;
  }
  //設(shè)置參數(shù)
  private void setParems(String[] parems){
    if(parems!=null){
      for(int i=0;i<parems.length;i++){
        try {
          pstmt.setString(i+1, parems[i]);
        } catch (SQLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  }
  public ResultSet executeQuery(String sql,String[] parems){
    ResultSet res = null;
    prepareConnection();
    try {
      pstmt = con.prepareStatement(sql);
      setParems(parems);
      res = pstmt.executeQuery();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
     }
    return res;
  }
}

學(xué)生類:

public class StudentBean {
  private long id;
  private String name;
  private String phone;
  private int age;
  private int score;
  public long getId() {
    return id;
  }
  public void setId(long id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getPhone() {
    return phone;
  }
  public void setPhone(String phone) {
    this.phone = phone;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public int getScore() {
    return score;
  }
  public void setScore(int score) {
    this.score = score;
  }
}

學(xué)生數(shù)據(jù)操作類

public class StudentDao implements StudentDaoIn {
@Override 
public ArrayList<StudentBean> findByPage(int page){
    DBBean db = new DBBean();
    int begin = (page-1) * 5;
    String sql = "select * from t_student limit "+begin+",5";
    ResultSet rs = db.executeQuery(sql,null);
    ArrayList<StudentBean> list = new ArrayList<StudentBean>();
    try {
      while(rs.next()){
        StudentBean st = new StudentBean();
        st.setName(rs.getString("name"));
        st.setAge(rs.getInt("age"));
        st.setId(rs.getInt("id"));
        st.setPhone(rs.getString("phnoe"));
        st.setScore(rs.getInt("score"));
        list.add(st);
      }
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return list;
  }
  @Override 
  public int userCount(){
    DBBean db = new DBBean();
    String sql = "select count(*) from t_student";
    ResultSet rs = db.executeQuery(sql, null);
    int count = 0;
    try {
      rs.next();
      count = rs.getInt(1);
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return count;
  }
}

相關(guān)業(yè)務(wù)邏輯

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String page = null;
    page = request.getParameter("page");
    if(page == null || page=="")
      page = "1";
    StudentDao studao = new StudentDao();
    request.setAttribute("student",studao.findByPage(Integer.parseInt(page)));
    request.setAttribute("pagenum",studao.userCount()/5+1);//總頁數(shù)
    request.setAttribute("page", page);//當(dāng)前頁
    request.getRequestDispatcher("student.jsp").forward(request, response);  
    
  }

前臺JSP代碼:

<table id="t_stu" border="1" cellpadding="2" cellspacing="0">
  <thead>
    <tr>
      <th>ID</th>
      <th>姓名</th>
      <th>年齡</th>
      <th>電話</th>
      <th>成績</th>
    </tr>
  </thead>
  <c:forEach items="${student}" var="st">
    <tr>
      <td>${st.getId()}</td>
      <td>${st.getName()}</td>
      <td>${st.getAge()}</td>
      <td>${st.getPhone()}</td>
      <td>${st.getScore()}</td>
    </tr>
  </c:forEach>
</table>
<br>
共 ${pagenum}頁  當(dāng)前 第${page}頁 
<c:choose>
  <c:when test="${page>1}">
    <a href="getSutent?page=${page-1}" rel="external nofollow" ><input type="button" value="上一頁" ></a>
  </c:when>
  <c:otherwise>
    <input type="button" value="上一頁" disabled="disabled" />
  </c:otherwise>
</c:choose>
<c:choose>
  <c:when test="${page!=pagenum}">
    <a href="getSutent?page=${page+1}" rel="external nofollow" ><input type="button" value="下一頁"></a>
  </c:when>
  <c:otherwise>
    <input type="button" value="下一頁" disabled="disabled" />
  </c:otherwise>
</c:choose>

本例是真分頁的一個(gè)簡單實(shí)現(xiàn),有著明顯的缺點(diǎn)。

例如:

1.在后臺相關(guān)業(yè)務(wù)邏輯處,只對page做了簡單的判斷,因?yàn)椴樵兿嚓P(guān)page時(shí),參數(shù)是寫入前臺a標(biāo)簽中的,所以懂技術(shù)的用戶,可以隨意改動(dòng)其值

由此查詢數(shù)據(jù)庫可能帶來意想不到的錯(cuò)誤。

2.功能不夠完善,僅提供了上一頁下一頁按鈕的簡單功能。

另外:實(shí)現(xiàn)假分頁時(shí)可以結(jié)合ajax和json。以此可實(shí)現(xiàn)無刷新翻頁,看起來功能和真分頁一樣。。。

相關(guān)文章

最新評論