/* * @(#)FastEmployeeList1.java * * Copyright (c) 1998 Karl Moss. All Rights Reserved. * * You may study, use, modify, and distribute this software for any * purpose provided that this copyright notice appears in all copies. * * This software is provided WITHOUT WARRANTY either expressed or * implied. * * @author Karl Moss * @version 1.0 * @date 04Apr98 * */ package javaservlets.db; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; /** *
This is a simple servlet that will use JDBC to gather all * of the employee information from a database and format it * into an HTML table. This servlet uses a local connection * pool. */ public class FastEmployeeList1 extends HttpServlet { // Our connection pool. Note that instance variables are // actually global to all clients since there is only // one instance of the servlet that has multiple threads // of execution javaservlets.jdbc.ConnectionPool m_connectionPool; /** *
Performs the HTTP GET operation * * @param req The request from the client * @param resp The response from the servlet */ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { // Set the content type of the response resp.setContentType("text/html"); // Create a PrintWriter to write the response java.io.PrintWriter out = new java.io.PrintWriter(resp.getOutputStream()); // Print the HTML header out.println(""); out.println("
"); out.println("Initialize the servlet. This is called once when the * servlet is loaded. It is guaranteed to complete before any * requests are made to the servlet * * @param cfg Servlet configuration information */ public void init(ServletConfig cfg) throws ServletException { super.init(cfg); // Create our connection pool m_connectionPool = new javaservlets.jdbc.ConnectionPool(); // Initialize the connection pool. This will start all // of the connections as specified in the connection // pool configuration file try { m_connectionPool.initialize(); } catch (Exception ex) { // Convert the exception ex.printStackTrace(); throw new ServletException ("Unable to initialize connection pool"); } } /** *
Destroy the servlet. This is called once when the servlet * is unloaded. */ public void destroy() { // Tear down our connection pool if it was created if (m_connectionPool != null) { m_connectionPool.destroy(); } super.destroy(); } /** *
Given the SQL query string, execute the query and
* format the results into an HTML table
*
* @param query SQL query to execute
* @param out PrintWriter to use to output the query results
* @return true if the query was successful
*/
private boolean query(String query,
java.io.PrintWriter out)
{
boolean rc = true;
// The JDBC Connection object
Connection con = null;
// The JDBC Statement object
Statement stmt = null;
// The JDBC ResultSet object
ResultSet rs = null;
// Keep stats for how long it takes to execute
// the query
long startMS = System.currentTimeMillis();
// Keep the number of rows in the ResultSet
int rowCount = 0;
try {
// Get an available connection from our connection pool
con = m_connectionPool.getConnection();
// Create a statement object that we can execute queries
// with
stmt = con.createStatement();
// Execute the query
rs = stmt.executeQuery(query);
// Format the results into an HTML table
rowCount = formatTable(rs, out);
}
catch (Exception ex) {
// Send the error back to the client
out.println("Exception!");
ex.printStackTrace(out);
rc = false;
}
finally {
try {
// Always close properly
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (con != null) {
// Put the connection back into the pool
m_connectionPool.close(con);
}
}
catch (Exception ex) {
// Ignore any errors here
}
}
// If we queried the table successfully, output some
// statistics
if (rc) {
long elapsed = System.currentTimeMillis() - startMS;
out.println("
" + rowCount + " rows in " +
elapsed + "ms");
}
return rc;
}
/**
*
Given a JDBC ResultSet, format the results into * an HTML table * * @param rs JDBC ResultSet * @param out PrintWriter to use to output the table * @return The number of rows in the ResultSet */ private int formatTable(java.sql.ResultSet rs, java.io.PrintWriter out) throws Exception { int rowCount = 0; // Create the table out.println("
" + rsmd.getColumnLabel(i + 1) + " | "); } // End the table row out.println("
---|
" + rs.getString(i + 1) + " | "); } // End the table row out.println("