Common Ajax skills-implementing database Paging

Source: Internet
Author: User

When you browse a webpage, you will often see pages displayed on pages. If you want to provide a large amount of data to the viewer, pagination is a very practical method. Pagination data helps viewers better view information and display information in a organized manner.
In traditional web technologies, page display operations are performed on the server side. The server side obtains the page for client requests and obtains the specified result set based on the number of request pages. Finally, the data in the result set is returned to the client. In this case, the returned results not only contain data, but may also contain the data display style. Each data update on the client will re-open a webpage. If the webpage contains many html elements, the webpage will be opened slowly.
To display part of the data, loading the data of the entire page is not worth the candle. Ajax can be used to make up for these problems. The server only transmits data in the database table, and the client obtains the data to update only partial content, keeping other elements unrelated to the data unchanged.
Create an instance to demonstrate how to display data by page using Ajax technology. The code implementation of this instance is divided into server and client.
1. Preparations

Here we use the Mysql database. I created a mobileshop table in the shop database. This table has two fields: name and model.
Open notepad and enter the following code:

<% @ 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.exe cuteQuery (SQL );
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
Public ResultSet getResultSet (){
Return rst;
}
}

%>

Save the preceding code as Conn. jsp to return the query result set.
2. server code

In this example, the server code can obtain the number of client request pages and generate a specified record set. Open notepad and enter the following code:

<% @ 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; // number of records displayed on one page
Int intRowCount; // The total number of records.
Int intPageCount; // the total number of pages.
Int intPage; // the page number to be displayed.
String strPage;
Int I;
IntPageSize = 2; // set the number of records displayed on one page
StrPage = request. getParameter ("page"); // obtain the page number to be displayed.
If (strPage = null) // judge whether strPage is equal to null. if yes, the first page of data is displayed.
{
IntPage = 1;
} Else {
IntPage = java. lang. Integer. parseInt (strPage); // convert the string to an Integer.
}
If (intPage <1)
{
IntPage = 1;
}
// Obtain the total number of records
Rs. last ();
IntRowCount = rs. getRow ();
// Calculate the total number of pages
IntPageCount = (intRowCount + intPageSize-1)/intPageSize;
// Adjust the displayed page number
If (intPage> intPageCount) intPage = intPageCount;
If (intPageCount> 0)
{
// Locate the record pointer to the first record on the page to be displayed.
Rs. absolute (intPage-1) * intPageSize + 1 );
}
// The following is used to display data:
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 ();
}
%>


Author: pingpang

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.