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
%>