JSP has long had offline data access like. Net dataset. Abandon resultset and embrace the result!

Source: Internet
Author: User

Microsoft. one feature of data access on the NET platform is that the data query results can be stored in the memory and described in XML format, without the need to keep online connections with the database, use DataSet + Data Adapter!

In JDBC, we usually use the javax. SQL. ResultSet class to store the stored data. Its Process and lifecycle are as follows:

Use ResultSet to return database query results
Client --> Connection --> Statement --> JDBC Driver -- +
  Database
Client <-- Parsing <-- ResultSet <-- JDBC Driver -- +
               
  Connection lifecycle
   
   
 
  ResultSet lifecycle  
 

We can see that this will occupy the database connection resources for a long time, which is a bit uncomfortable...

In fact, another mechanism is provided in JSTL, so that we can use it offline when returning the query results to the presentation layer! It is the javax. servlet. jsp. jstl. SQL. Result class!

1 <% @ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
2 <% @ page contentType = "text/html; charset = UTF-8" %>
3 <% @ pageImport = "java. SQL. Connection" %>
4 <% @ pageImport = "java. SQL. DriverManager" %>
5 <% @ pageImport = "java. SQL. ResultSet" %>
6 <% @ pageImport = "java. SQL. SQLException" %>
7 <% @ pageImport = "java. SQL. Statement" %>
8 <% @ pageImport = "javax. servlet. jsp. jstl. SQL. Result" %>
9 <% @ pageImport = "javax. servlet. jsp. jstl. SQL. ResultSupport" %>
10 <%
11 // now I think of the following content as a DAO in a multi-layer architecture. I am lazy!
12 Connection conn =Null;
13 Statement stmt =Null;
14 ResultSet rs =Null;
15 String strDbUrl
16 = "jdbc: mysql: // localhost/tutorial? User = tutorial & password = tutpwd ";
17Try {
18 // start querying the database
19 Class. forName("Com. mysql. jdbc. Driver"). NewInstance();
20 conn = DriverManager. getConnection(StrDbUrl);
21 stmt = conn. createStatement();
22 String strSql = "SELECT * FROM R_TUT_USERS ";
23 rs = stmt.exe cuteQuery(StrSql);
24 // convert ResultSet to Result
25Result userdata = resultsupport. toresult (RS );
26 // when we put the result into a Model bean, we can close the database connection.
27 // to simplify the process, we can now regard pageContext as the Model Bean!
28 pageContext. setAttribute("UserData", userData);
29}
30Catch (Exception ex) {
31 // handle any errors
32 system. Out. println("Sqlexception:" + ex. getmessage());
33}
34Finally {
35Try {
36If (RS! =Null) {
37 Rs. Close();
38}
39If (Stmt! =Null) {
40 stmt. Close();
41}
42If (Conn! =Null) {
43 conn. Close();
44}
45}
46Catch (Sqlexception ex) {
47 system. Out. println("SQL exception:" + ex. getmessage());
48}
49}
50 // logic end of Dao
51%>
52 <HTML>
53 54 <title> test </title>
55 56 <body>
57 <! -- // The following is the presentation layer. // -->
58 <c: forEach items = "$ {userData. rows}" var = "user">
59 <c: out value = '$ {user. name}'/>
60 </c: forEach>
61 </body>
62
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.