JSP+MYSQL+Java類最佳化分頁的執行個體

來源:互聯網
上載者:User

在JSP中經常要用到查詢資料庫中的資料,同常我們的做法是使用SQL語句“select * from tablename order by id desc”,這樣的做法有 一個缺點,當資料庫很大的時候查詢的速度會變的很慢,在ASP中有一種方法 "select top "&recpage&" * from tablename where id not in (select top "&(recpage*(currentpage-1))&" id from products order by id desc) order by id desc"其中recpage為每 頁顯示個數, currentpage為當前頁數.不過在MYSQL資料庫中沒有“select top * " 語句,而可以代替的語句是”select * from tablename limit position, counter “position 指示從哪裡開始查詢,如果是0則是從頭開始,counter 表示查詢的個數,通過JSP+JAVA查詢資料庫,查 詢擷取的資料暫時存放在記憶體中在JSP中通過調取JAVA類,直接從記憶體中提取資料,速度有了很大提高。

下面的例子是一個關於網友評論的部分程式,假如你有一個專門供網友瀏覽的網站,而現在又想和網友互動起來,加一個評論是不錯的想法 ,那麼你可以把下面的程式加上,建一個表其中加一個photo_id欄位和你的表關聯起來後,就可以讓網友對你的圖片點評了。

Comment.java是一個評論的類

//<--------Comment.java ------->
package dbconnection;
public class Comment
{
  private String id;
  private String album_id;
  private String title;
  private String content;
  private String modi_time;
  private String user;
  public void setId(String ids)
  {
   this.id=ids;
  }
  public void setalbum_id(String album_ids)
  {
   this.album_id=album_ids;
  }
  public void setTitle(String titles)
  {
   this.title=titles;
  }
  public void setContent(String contents)
  {
   this.content=contents;
  }
  public void setModi_time(String modi_times)
  {
   this.modi_time=modi_times;
  }
  public void setUser(String users)
  {
   this.user=users;
  }
  public String getId()
  {
   return id;
  }
  public String getalbum_id()
  {
   return album_id;
  }
  public String getTitle()
  {
   return title;
  }
  public String getContent()
  {
   return content;
  }
  public String getModi_time()
  {
   return modi_time;
  }
  public String getUser()
  {
   return user;
  }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.