Ajax的常用技巧---實現資料庫分頁

來源:互聯網
上載者:User

在瀏覽網頁時,經常會看到分頁顯示的頁面。如果想把大量的資料提供給瀏覽者,分頁顯示是個非常實用的方法。分頁顯示資料能夠協助瀏覽者更好地查看資訊,能夠有條理的顯示資訊。
在傳統的web技術中,分頁顯示的相關操作都是在伺服器端進行的,伺服器端擷取用戶端的請求分頁,並根據請求頁數擷取指定的結果集。最後把結果集中的資料返回到用戶端,這時返回結果中不但包含了資料,還可能包含了資料的顯示樣式。用戶端的每一次資料更新,都會重新開啟一個網頁,如果網頁中包含了很多html元素,就會造成網頁開啟速度較慢的情況。
為了顯示部分資料,而需要載入整個頁面的資料,顯得有點得不償失。使用Ajax技術可以很好的彌補這些問題,伺服器端只傳輸資料庫表中的資料,用戶端擷取這些資料只更新局部內容,與資料無關的其他元素保持不變。
現在建立一個執行個體,以示範使用Ajax技術實現資料的分頁顯示。該執行個體的代碼實現分為伺服器端和用戶端。
1,準備工作

我們這裡使用Mysql資料庫,我在shop資料庫中建立了一張mobileshop表,這張表有兩個欄位name,model。
開啟記事本,輸入下列代碼:

<%@ page language="java" import="java.util.*,java.sql.*,java.io.*" pageEncoding="GBK"%>
<%
 class DBManager{
     String userName="root";
    String password="123456";
    Connection conn=null;
    Statement stmt=null;
    String url="jdbc:mysql://localhost:3306/shop";
    ResultSet rst;
public DBManager(String sql){

    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn=DriverManager.getConnection(url,userName,password);
        stmt=conn.createStatement();
        rst=stmt.executeQuery(sql);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
}
public ResultSet getResultSet(){
return rst;
}
}

%>

將上述代碼儲存為Conn.jsp,用於返回查詢結果集。
2,伺服器端代碼

     在本執行個體中,伺服器端代碼具有擷取用戶端請求頁數和產生指定記錄集的功能。開啟記事本,輸入下列代碼:

<%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="" %>
<%@ include file="Conn.jsp" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%
 try
 {
ResultSet rs=new DBManager("select name,model from mobileshop").getResultSet();
int intPageSize;      //一頁顯示的記錄數
int intRowCount;      //記錄的總數
int intPageCount;     //總頁數
int intPage;         //待顯示的頁碼
String strPage;
int i;
intPageSize=2;       //設定一頁顯示的記錄數
strPage=request.getParameter("page");         //取得待顯示的頁碼
if(strPage==null)             //判斷strPage是否等於null,如果是,則顯示第一頁資料
{
intPage=1;
}else{
intPage=java.lang.Integer.parseInt(strPage);   //將字串轉化為整形
}
if(intPage<1)
{
intPage=1;
}
//擷取記錄總數
rs.last();
intRowCount=rs.getRow();
//計算總頁數
intPageCount=(intRowCount+intPageSize-1)/intPageSize;
//調整顯示的頁碼
if(intPage>intPageCount) intPage=intPageCount;
if(intPageCount>0)
{
//將記錄指標定位到待顯示頁的第一條記錄上
rs.absolute((intPage-1)*intPageSize+1);
}
//下面用於顯示資料
i=0;
  StringBuffer content=new StringBuffer("");
  response.setContentType("text/xml");
  response.setHeader("Cache-Control","no-cache");
  content.append("<?xml version=\"1.0\"   encoding=\"UTF-8\" ?>");
  content.append("<contents>");
while(i<intPageSize && !rs.isAfterLast())
{
    
     String name=rs.getString("name");
     String email=rs.getString("model");
     content.append("<content>");
     content.append("<name>"+ name +"</name>");
     content.append("<model>"+email+"</model>");
     content.append("</content>");
 rs.next();
 i++;www.2cto.com
 }
 content.append("</contents>");
 System.out.print(content);
 out.print(content);
 }
 catch(Exception e)
 {
 e.printStackTrace();
 }
 %>


作者:pingpang

聯繫我們

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