title: jsp page query display common mode
Author: evan
Email: evan_zhao@hotmail.com
background :
1. you need to display the results of the database query as a list in the JSP
2. in a good Java EE mode database queries are generally used with DAO implementations (Data access object), jsp only for displaying data
problem :
jdbc resultset to get query results (existing in the database buffer), but in statement, ResultSet is not available when the connection is closed. So you need a way to fetch all the query results and pass it to the JSP page.
Workaround one :
use Value object. Each record is encapsulated into a JavaBean object, and the objects are loaded into collection to be transmitted to the JSP display. The disadvantage of this approach is that each query needs to define a java class, and it requires a lot of extra code to encapsulate the record data into Java objects.
Sample code:
-
- //query data code
- connection conn = dbutil.getconnection ();
- preparedstatement pst = null ;
- resultset rs = null ;
- try {
- Font class= "Classlink" > String sql= "Select emp_code, real_name from t_employee where organ_id=? ";
- pst = conn.preparedstatement (SQL);
- pst.setstring (1, "");
- resultset rs = pst.executequery ();
- list list = new arraylist ();
- employee emp;
- while (Rs.next ()) {
-    &NBSP