Common query and display methods and analysis of jsp pages
This document describes common queries and display methods for jsp pages. We will share this with you for your reference. The details are as follows:
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.
Method 1:
Create a class, encapsulate the query results into the class, and then add the Class Object to the List. (This is also the method I used at the beginning. It is not common and too troublesome ).
Method 2:
When introducing method 2, Let's first look at how to convert ResultSet to List. 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 the ResultSet to retrieve all data and encapsulate it into the Collection.
Specific Practices:
1. Generate a List object (List list = new ArrayList ()).
2. Generate a Map object (Map map = new HashMap ()). Use Map to encapsulate a row of data. The key is the name of each field and the value is the corresponding value. (Map. put ("USER_NAME"), rs. getString ("USER_NAME "))
3. Load the Map object generated in step 1 into the list object (list. add (map) in step 2 )).
4. Repeat steps 2 and 3 until the ResultSet traversal is complete.
The preceding procedure is implemented in the DBUtil. resultSetToList (ResultSet rs) method (uppercase is used for all column names.
Sample Code:
// Code for querying data :... 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"); rs = pst.exe cuteQuery (); List list = DBUtil. resultSetToList (ResultSet rs); return list;} finally {DBUtil. close (rs, pst, conn);} // JSP shows some 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> <% = colMap. get ("REAL_NAME") %> </td> </tr> <%} // end for %> </table>
Solution 3:
Use RowSet.
RowSet is an interface provided in JDBC2.0. Oracle implements this interface accordingly. It is useful for oracle. jdbc. rowset. OracleCachedRowSet. OracleCachedRowSet implements all the methods in ResultSet. However, unlike ResultSet, data in OracleCachedRowSet remains valid after Connection is disabled.
The oracle rowset is implemented in the jdbcdownload of http://otn.oracle.com/software/content.html. it is named ocrs12.zip.
Sample Code:
// Code for querying data: 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," 101 "); rs = pst.exe cuteQuery (); OracleCachedRowSet ors = newOracleCachedRowSet ();
I hope this article will help you with jsp program design.