JAVA/JSP Learning Series 5 (JDBC-ODBC flip example) _ MySQL

Source: Internet
Author: User
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 );

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.