1. before running, we recommend that you use MSSQLServer7 database DNS. The name is: 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 % @ pagecontentTypetexthtml; charsetgb2312 % variable declaration JDBCODBC
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 the JDBC-ODBC driver
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 );