There is already an offline data access like. Net dataset in the JSP

Source: Internet
Author: User
Tags foreach getmessage sql mysql net string stmt access
js| Access | data

JSP already has a similar. Net dataset like the offline data access, abandon resultset, hug result!

Microsoft's. NET platform above the data access has a feature, that is, the results of data query, can be placed in memory, in the form of XML description, do not have to keep online with the database connection, with DataSet + Data adapter to achieve!

In JDBC, we usually use the Javax.sql.ResultSet class to store the data that is put back, and 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 take a long time to occupy the resources of the database connection, is a bit of a problem ...

In fact, in the JSTL provides another mechanism, let us return query results to the presentation layer, you can do offline use! 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 <% @page import= "Java.sql.Connection"%>
4 <% @page import= "Java.sql.DriverManager"%>
5 <% @page import= "Java.sql.ResultSet"%>
6 <% @page import= "Java.sql.SQLException"%>
7 <% @page import= "Java.sql.Statement"%>
8 <% @page import= "Javax.servlet.jsp.jstl.sql.Result"%>
9 <% @page import= "Javax.servlet.jsp.jstl.sql.ResultSupport"%>
Ten <%
11//For the moment, consider this below as the DAO in the multi-layer architecture okay, I'm slacking off!
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String Strdburl
= "Jdbc:mysql://localhost/tutorial?user=tutorial&password=tutpwd";
The try {
18//Start with the database query Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
conn = Drivermanager.getconnection (strdburl); stmt = Conn.createstatement ();
String strSQL = "SELECT * from R_tut_users"; n rs = stmt.executequery (strSQL);
24//Convert resultset into Result25 result userData = Resultsupport.toresult (RS);
26//When we put the results in a model bean, we can close the database connection.
27//In order to simplify, we will consider PageContext as model Bean for now!
Pagecontext.setattribute ("UserData", userData);
29}
catch (Exception ex) {
To//handle any errors
System.out.println ("SQLException:" + ex.getmessage ());
33}
finally {
A try {
(Rs!= null) {
Panax Notoginseng rs.close ();
38}
if (stmt!= null) {
Stmt.close ();
41}
IF (conn!= null) {
Conn.close ();
44}
45}
A catch (SQLException ex) {
System.out.println ("SQL Exception:" + ex.getmessage ());
48}
49}
The logical end of//dao
Wuyi%>
<title>test</title>
<body>
<!--//below as a presentation layer bar//-->
<c:foreach items= "${userdata.rows}" var= "User" >
<c:out value= ' ${user.name} '/>
</c:forEach>
</body>



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.