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)