Java Reference
In-Depth Information
L ISTING 20.9
Continued
throw new Exception(“ResultSet is null”);
}
int count = 0;
while ( rs.next() ) {
HashMap row = new HashMap();
for ( int x = 0; x < columnNames.size(); x++ ) {
String rsColumn = (String)columnNames.elementAt(x);
String rsValue = rs.getString(rsColumn);
if ( rsValue == null ) {
rsValue = new String(“”);
}
row.put(rsColumn, rsValue);
}
results.add(row);
}
// Return the result
return results.toArray();
}
/*
implemented method from Service interface
*/
public void execute(HttpServletRequest request,
HttpServletResponse response,
ServletContext context) throws Exception {
// Get category_id from the request
String catid = request.getParameter(“catid”);
// Get a database connection from CATALOG_POOL
ConnectionPool pool =
(ConnectionPool)context.getAttribute(“CATALOG_POOL”);
Connection con = pool.getConnection();
try {
if ( con != null ) {
Statement statement = con.createStatement();
// Get titles with matching category id
StringBuffer query = new StringBuffer(“SELECT * FROM TITLES”);
query.append(“ WHERE CATEGORY_ID = “ + catid);
ResultSet rs =
statement.executeQuery(query.toString());
Object[] movies = parseResultSet(rs);
// Add the resulting movies to the request
request.setAttribute(“movies”, movies);
Search WWH ::




Custom Search