JAVA/JSP Learning Series 8 (rewrite MySQL paging example)

Source: Internet
Author: User

I. Preface

In fact, the modified JDBC Data-Source runs in the Servlet and uses JNDI to find the Data Source. I tried it with Orion and simply rewritten this article "6 of the JAVA/JSP Learning Series (MySQL paging example.

Ii. Configuration

(1) JDBC

Copy the JDBC driver to the [ORION]/lib directory.

(2) data-source

Add the following to the [ORION]/config/data-sources.xml file:

<Data-source

Class = "com. evermind. SQL. DriverManagerDataSource"

Name = "mySqlDbpage"

Location = "jdbc/HypersonicCoreDS"

Xa-location = "jdbc/xa/HypersonicXADS"

Ejb-location = "jdbc/mysqlDbPage"

Connection-driver = "org. gjt. mm. mysql. Driver"

Username = "root"

Password = ""

Url = "jdbc: mysql: // localhost/test"

Inactivity-timeout = "30"

/> /〉

Note that:

(1) jdbc/mysqlDbPage after ejb-location is to be searched by JNDI.

(2) connection-driver is the JDBC database driver.

(3) The url is the URL in JDBC.

(4) username is the Database User Name

(5) password: User password

(6) inactivity-timeout indicates that the database connection times out. The default value is 30 seconds.

Do not change it elsewhere.

3. The modified code is as follows:

<% @ Page contentType = "text/html; charset = gb2312" %>

<% @ Page import = "java. SQL. *, javax. SQL. DataSource, javax. naming. InitialContext" %>

<%

// Create a JNDI lookup object

InitialContext JNDI_Context = new InitialContext ();

// Query the data source using JNDI

DataSource ds = (DataSource) JNDI_Context.lookup ("jdbc/mysqlDbPage ");

// Obtain a data source connection

Connection conn = ds. getConnection ();

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.

Java. lang. String strPage;

Int I;

// Set the number of records displayed on one page

IntPageSize = 2;

// Obtain the page number to be displayed

StrPage = request. getParameter ("page ");

If (strPage = null ){

// Indicates that the QueryString parameter does not contain the page parameter. The first page of data is displayed.

IntPage = 1;

} Else {

// Converts a string to an integer.

IntPage = java. lang. Integer. parseInt (strPage );

If (intPage <1) intPage = 1;

}

// Obtain the result

Stmt = conn. createStatement ();

ResultSet sqlRst = stmt.exe cuteQuery ("select f1 from test ");

// Obtain the total number of records

SqlRst. last ();

IntRowCount = sqlRst. getRow ();

// Calculate the total number of pages

IntPageCount = (intRowCount + intPageSize-1)/intPageSize;

// Adjust the page number to be displayed

If (intPage> intPageCount)

IntPage = intPageCount;

%>

<Html>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">

<Title> JSP database operation routine-data paging-JDBC 2.0-mysql </title>

</Head>

<Body>

<Table border = "1" cellspacing = "0" cellpadding = "0">

<Tr>

<Th> name </th>

</Tr>

<% If (intPageCount> 0)

{

// Locate the record pointer to the first record on the page to be displayed.

SqlRst. absolute (intPage-1) * intPageSize + 1 );

// Display data

I = 0;

While (I <intPageSize &&! SqlRst. isAfterLast () {%>

<Tr>

<Td>

<% = SqlRst. getString (1) %>

</Td>

</Tr>

<% SqlRst. next ();

I ++;

}

}

%>

</Table>

Page <% = intPage %> total page <% = intPageCount %>

<% If (intPage <intPageCount) {%> <a href = "mysqlpage. jsp? Page = <% = intPage + 1%> "> next page </a> <%} %>

<% If (intPage> 1) {%> <a href = "mysqlpage. jsp? Page = <% = intPage-1 %> "> previous page </a> <%} %>

</Body>

</Html>

<%

// Close the result set

SqlRst. close ();

%>

3. How to run it?

See the previous article "JAVA/JSP learning series five (JDBC-ODBC flip example)".

Note: the MySQL database is test, with a table test in the middle and a field f1 (varchar)

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.