Common display modes of JSP page query (1)

Source: Internet
Author: User

Background:

1. List the database query results in JSP

2. In a good J2EE mode, Data Access Object is usually implemented using DAO for database queries. JSP is only used to display Data.

Solution 1:

Use Value Object. encapsulate each record into a JavaBean Object and load these objects into a Collection Object and send them to JSP for display. The disadvantage of this method is that a java class needs to be defined for each query and a lot of additional code is required to encapsulate record data into a java object.

Sample Code:

// Query the data 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, 101 ");
ResultSet rs = pst.exe cuteQuery ();
List list = new ArrayList ();
Employee emp;
While (rs. next ()){
Emp = new Employee ();
Emp. setReakName (rs. getString ("real_name "));
Emp. setEmpCode (rs. getString ("emp_code "));
...
List. add (emp );
}
Return list;
} Finally {
DBUtil. close (rs, pst, conn );
}
// Display part of the jsp code
<%
List empList = (List) request. getAttribute ("empList ");
If (empList = null) empList = Collections. EMPTY_LIST;
%>
...

Code Name
<%
Employee emp;
For (int I = 0; I <empList. size (); I ++ ){
Emp = (Employee) empList. get (I );
%>

<% = Emp. getEmpCode () %>
<% = Emp. getRealName () %>

<%
} // End
%>


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.