JSP page commonly used query and display method analysis _jsp programming

Source: Internet
Author: User

This article illustrates the common query and display methods of JSP pages. Share to everyone for your reference, specific as follows:

Background:

1. You need to display the results of a database query as a list in a JSP
2. In a good Java EE mode, database queries are generally implemented with DAO (Data Access Object), and JSP is used only for displaying data

Method One:

Build a class that encapsulates the results of the query into the class, and then adds the class object to the list. (This is also the first time I used the method, not universal and too troublesome).

Method Two:

In the introduction of method two, we first look at how to convert ResultSet to list bar, the code is as follows:

private static List Resultsettolist (ResultSet rs) throws SQLException {
 List list = new ArrayList ();
 ResultSetMetaData MD = Rs.getmetadata ();
 int columnCount = Md.getcolumncount ();
 while (Rs.next ()) {
  Map rowdata = new HashMap ();
  for (int i = 1; I <= columnCount i++) {
  rowdata.put (md.getcolumnname (i), rs.getobject (i));
  }
  List.add (RowData);
 }
 return list;
}

Traverse resultset to remove all data encapsulated into collection.

Specific practices:

1. Generates a List object (List List = new ArrayList ()).
2. Generates a Map object (map map = new HashMap ()). Use map to encapsulate one row of data, key is each field name, value is the corresponding value. (Map.put ("user_name"), rs.getstring ("user_name"))
3. Load the Map object generated in step 2nd into the list object in step 1th (List.add (map)).
4. Repeat 2, 3 steps until resultset traversal.

In Dbutil. This procedure is implemented in the Resultsettolist (ResultSet Rs) method (all column names are capitalized) and can be referenced using.

Sample code:

Query data part of the code: ...
Connection conn = Dbutil.getconnection ();
PreparedStatement PST = NULL;
ResultSet rs = null;
try{
String sql= "Select Emp_code, Real_name from T_employee where organ_id=?";
PST = conn.preparedstatement (SQL);
Pst.setstring (1, "a");
rs = Pst.executequery ();
List List = Dbutil. Resultsettolist (ResultSet rs);
return list;
} finally{
Dbutil.close (RS, PST, conn);
}
JSP display part of
the code <%
List emplist = (list) request.getattribute ("Emplist");
if (emplist = = null) Emplist = collections.empty_list;
%> ...
<table cellspacing= "0" width= "90%" >
<tr> <td> code </td> <td> name </td> </tr >
<%
Map colmap;
for (int i=0; i< emplist.size (); i++) {
Colmap = (Map) emplist.get (i);
%>
<tr>
<td><%=colmap.get ("Emp_code")%></td>
<td><%= Colmap.get ("Real_name")%></td>
</tr>
<%
}//end for
%>
</table >

Workaround Three:

Use rowset.

Rowset is the interface provided in JDBC2.0, and Oracle has a corresponding implementation of the interface, which is useful for oracle.jdbc.rowset.OracleCachedRowSet. Oraclecachedrowset implements all the methods in resultset, but unlike ResultSet, the data in Oraclecachedrowset is still valid after the connection is closed.

Oracle's rowset implementation is available in the http://otn.oracle.com/software/content.html jdbc download, the name is Ocrs12.zip

Sample code:

Query data Part code:
import Javax.sql.RowSet;
Import Oracle.jdbc.rowset.OracleCachedRowSet;
... Connection conn = Dbutil.getconnection ();
PreparedStatement PST = NULL;
ResultSet rs = null;
try{...
String sql= "Select Emp_code, Real_name from T_employee where organ_id=?";
PST = conn.preparedstatement (SQL);
Pst.setstring (1, "a");
rs = Pst.executequery ();
Oraclecachedrowset ors = Neworaclecachedrowset ();

I hope this article will help you with JSP program design.

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.