Title: Common display modes for JSP page queries
Author: evan
Email: evan_zhao@hotmail.com
Background:
1. List the database query results in JSP
2. In a good J2EE mode, database queries are generally implemented using DAO (Data Access Object). JSP is only used to display Data.
Problem:
You can use JDBC ResultSet to obtain the query results (stored in the database buffer). However, after Statement and Connection are disabled, ResultSet is unavailable. Therefore, you need to retrieve all the query results and pass them to the JSP page.
Solution 1:
Use Value Object. Encapsulate each record into a JavaBean object and load these objects into a Collection file 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{
- StringSQL = "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 =NewArrayList ();
- Employee emp;
- While(Rs. next ()){