JSP database operation routine (JDBC-ODBC)

Source: Internet
Author: User

To achieveJSP database operation instanceCreates 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.

Code

 
 
  1. <% @ PageContentType="Text/html; charset = gb2312"%> 
  2. <%
  3. // Variable Declaration
  4. Java. SQL. Connection sqlCon; // database Connection object
  5. Java. SQL. Statement sqlStmt; // SQL Statement object
  6. Java. SQL. ResultSet sqlRst; // result set object
  7. Java. lang. String strCon; // database connection String
  8. Java. lang. String strSQL; // SQL statement
  9. Int intPageSize; // number of records displayed on one page
  10. Int intRowCount; // The total number of records.
  11. Int intPageCount; // the total number of pages.
  12. Int intPage; // the page number to be displayed.
  13. Java. lang. String strPage;
  14. Int I, j, k; // set the number of records displayed on one page
  15. IntPageSize=5; // Obtain the page number to be displayed
  16. StrPage=Request. GetParameter ("page ");
  17. If (StrPage= Null ){
  18. // Indicates that the QueryString parameter does not contain the page parameter. The first page of data is displayed.
  19. IntPage=1;
  20. } Else {
  21. // Converts a string to an integer.
  22. IntPage=Java. Lang. Integer. parseInt (strPage );
  23. If (intPage< 1)IntPage=1;}
  24. // Load the JDBC-ODBC driver
  25. Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
  26. // Set the database connection string
  27. StrCon="Jdbc: odbc: Test_DB";
  28. // Connect to the database
  29. SqlCon=Java. SQL. DriverManager. getConnection (strCon, "sa ","");
  30. // Create an SQL statement object
  31. SqlStmt=SqlCon. CreateStatement ();
  32. // Obtain the total number of records
  33. StrSQL="Select count (*) from guestbook";
  34. SqlRst=SqlStmt. ExecuteQuery (strSQL );
  35. // Execute the SQL statement and obtain the result set
  36. SqlRst. next (); // when the record set is opened, the pointer is located before the first record.
  37. IntRowCount=SqlRst. GetInt (1 );
  38. SqlRst. close (); // close the result set.
  39. // Calculate the total number of pages
  40. IntPageCount= (IntRowCount + intPageSize-1)/intPageSize;
  41. // Adjust the page number to be displayed if (intPage>IntPageCount)IntPage=IntPageCount;
  42. // Set the SQL statement for getting data
  43. StrSQL="Select name, email, body from guestbook";
  44. // Execute the SQL statement and obtain the result set
  45. SqlRst=SqlStmt. ExecuteQuery (strSQL );
  46. // Locate the record pointer to the first record on the page to be displayed.
  47. I= (IntPage-1) * intPageSize;
  48. For (J=0; J< I; J ++) sqlRst. next (); %> 
  49. < Html> 
  50. < Head> 
  51. < Title>JSP database operation routine-data paging display-JDBC-ODBC</Title> 
  52. </Head> 
  53. < Body> 
  54. < P Align=Center>Jdbc-odbc message board</P> 
  55. < Table Border="1" Cellspacing="0" Cellpadding="0" Width=600 Align=Center> 
  56. <%
  57. // Display data
  58. I=0;
  59. While (I< IntPageSize& SqlRst. next () {%> 
  60. < Tr> 
  61. < Td>Name:<% = SqlRst. getString (1) %></Td> 
  62. < Td>Email:<% = SqlRst. getString (2) %></Td> 
  63. </Tr> 
  64. < Tr> 
  65. < Td Colspan=2><% = SqlRst. getString (3) %></Td> 
  66. </Tr> 
  67. <% I ++;} %> 
  68. < Tr> 
  69. < Td Colspan=2 Align=Center> 
  70. The<% = IntPage %>Total pages<% = IntPageCount %>Page
  71. <% If (intPage< IntPageCount) {%> 
  72. < A Href="Mssql. jsp? Page = <% = intPage + 1%>">Next Page</><%
  73. }
  74. %> 
  75. <% If (intPage>1) {%> 
  76. < A Href="Mssql. jsp? Page = <% = intPage-1 %>">Previous Page</><%
  77. }
  78. %> 
  79. </Td> 
  80. </Tr> 
  81. </Table> </Body> 
  82. </Html> 
  83. <%
  84. // Close the result set
  85. SqlRst. close ();
  86. // Close the SQL statement object
  87. SqlStmt. close ();
  88. // Close the database
  89. SqlCon. close ();
  90. %> 

How to run JSP database operations?

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:

 
 
  1. http://localhost:port/test.jsp 

Access Test

You can run Resin, Tomcat, JWS, and so on. This completes the JSP database operation.

  1. Basic preparation for learning JSP: JDK installation and configuration
  2. Java Servlet Introduction: Perfect Combination with JSP
  3. Integrate JSP and PHP in Apache
  4. Java Servlets (JSP) Development Environment
  5. Develop jsp http Server

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.