I. Preparations before running
We recommend that you create a DNS for the MS sqlserver7 database named test_db.
There is a table in the database: Guestbook field: Name (varchar), email (varchar), body (text)
The SA password is blank for database users and can be modified by themselves.
II,Code
<% @ Page contenttype = "text/html; charset = gb2312" %>
<%
// Variable Declaration
Java. SQL. Connection sqlcon; // database connection object
Java. SQL. Statement sqlstmt; // SQL statement object
Java. SQL. resultset sqlrst; // result set object
Java. Lang. String strcon; // database connection string
Java. Lang. String strsql; // SQL statement
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, j, k; // set the number of records displayed on one page
Intpagesize = 5; // get 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 ;}
// Load JDBC-ODBC driveProgram
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
// Set the database connection string
Strcon = "JDBC: ODBC: test_db ";
// Connect to the database
Sqlcon = java. SQL. drivermanager. getconnection (strcon, "sa ","");
// Create an SQL statement object
Sqlstmt = sqlcon. createstatement ();
// Obtain the total number of records
Strsql = "select count (*) from Guestbook ";
Sqlrst = sqlstmt.exe cutequery (strsql );
// Execute the SQL statement and obtain the result set
Sqlrst. Next (); // when the record set is opened, the pointer is located before the first record.
Introwcount = sqlrst. getint (1 );
Sqlrst. Close (); // close the result set.
// Calculate the total number of pages
Intpagecount = (introwcount + intPageSize-1)/intpagesize;
// Adjust the page number to be displayed if (intpage> intpagecount) intpage = intpagecount;
// Set the SQL statement for getting data
Strsql = "Select name, email, body from Guestbook ";
// Execute the SQL statement and obtain the result set
Sqlrst = sqlstmt.exe cutequery (strsql );
// Locate the record pointer to the first record on the page to be displayed.
I = (intPage-1) * intpagesize;
For (j = 0; j <I; j ++) sqlrst. Next (); %>
<HTML>
<Head>
<Title> JSP database operation routine-data paging display-JDBC-ODBC </title>
</Head>
<Body>
<P align = center> JDBC-ODBC message board </P>
<Table border = "1" cellspacing = "0" cellpadding = "0" width = 600 align = center>
<%
// Display data
I = 0;
While (I <intpagesize & sqlrst. Next () {%>
<Tr>
<TD> name: <% = sqlrst. getstring (1) %> </TD>
<TD> Email: <% = sqlrst. getstring (2) %> </TD>
</Tr>
<Tr>
<TD colspan = 2> <% = sqlrst. getstring (3) %> </TD>
</Tr>
<% I ++ ;}%>
<Tr>
<TD colspan = 2 align = center>
Page <% = intpage %> total page <% = intpagecount %>
<% IF (intpage <intpagecount) {%>
<A href = "MSSQL. jsp? Page = <% = intpage + 1%> "> next page </a> <%
}
%>
<% IF (intpage> 1) {%>
<A href = "MSSQL. jsp? Page = <% = intPage-1 %> "> previous page </a> <%
}
%>
</TD>
</Tr>
</Table> </body>
</Html>
<%
// Close the result set
Sqlrst. Close ();
// Close the SQL statement object
Sqlstmt. Close ();
// Close the database
Sqlcon. Close ();
%>
3. How to run it?
Save the code as a file test. jsp
Under Orion Application Server:
Copy to the default-web-app directory of Orion, and run the following command:
Http: // localhost: Port/test. jsp
Access Test
You can run resin, tomcat, JWS, and so on.