Example of JDBC-ODBC flip

Source: Internet
Author: User


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 );



// 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.

Related Article

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.