import java.io.*; import java.net.URL; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; /** * This is a simple example of an HTTP Servlet. It responds to the GET * and HEAD methods of the HTTP protocol. */ public class SimpleServlet extends HttpServlet { /** * Handle the GET and HEAD methods by building a simple web page. * HEAD is just like GET, except that the server returns only the * headers (including content length) not the body we write. */ public void doGet ( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { PrintWriter out; String title = "Simple Servlet Output"; // set content type and other response header fields first response.setContentType("text/html"); // then write the data of the response out = response.getWriter(); out.println(""); out.println(title); out.println(""); out.println("

" + title + "

"); out.println("

This is output from SimpleServlet.

"); String url = "jdbc:odbc:Cust"; String query = "SELECT * FROM Customer ORDER BY UID ASC"; try { // Load the jdbc-odbc bridge driver Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); // Attempt to connect to a driver. Each one // of the registered drivers will be loaded until // one is found that can process this URL Connection con = DriverManager.getConnection ( url, "Cust", "Cust"); // If we were unable to connect, an exception // would have been thrown. So, if we get here, // we are successfully connected to the URL // Check for, and display and warnings generated // by the connect. checkForWarning (con.getWarnings (), out); // Get the DatabaseMetaData object and display // some information about the connection DatabaseMetaData dma = con.getMetaData (); out.println("\nConnected to " + dma.getURL()); out.println("Driver " + dma.getDriverName()); out.println("Version " + dma.getDriverVersion()); out.println("

"); // Create a Statement object so we can submit // SQL statements to the driver Statement stmt = con.createStatement (); // Submit a query, creating a ResultSet object ResultSet rs = stmt.executeQuery (query); // Display all columns and rows from the result set dispResultSet (rs, out); // Close the result set rs.close(); // Close the statement stmt.close(); // Close the connection con.close(); } catch (SQLException ex) { // A SQLException was generated. Catch it and // display the error information. Note that there // could be multiple error objects chained // together out.println ("\n*** SQLException caught ***\n"); while (ex != null) { out.println ("SQLState: " + ex.getSQLState ()); out.println ("Message: " + ex.getMessage ()); out.println ("Vendor: " + ex.getErrorCode ()); ex = ex.getNextException (); out.println (""); } } catch (java.lang.Exception ex) { // Got some other type of exception. Dump it. ex.printStackTrace (); } out.println(""); out.close(); } //------------------------------------------------------------------- // checkForWarning // Checks for and displays warnings. Returns true if a warning // existed //------------------------------------------------------------------- private static boolean checkForWarning (SQLWarning warn, PrintWriter out) throws SQLException { boolean rc = false; // If a SQLWarning object was given, display the // warning messages. Note that there could be // multiple warnings chained together if (warn != null) { out.println ("\n *** Warning ***\n"); rc = true; while (warn != null) { out.println ("SQLState: " + warn.getSQLState ()); out.println ("Message: " + warn.getMessage ()); out.println ("Vendor: " + warn.getErrorCode ()); out.println (""); warn = warn.getNextWarning (); } } return rc; } //------------------------------------------------------------------- // dispResultSet // Displays all columns and rows in the given result set //------------------------------------------------------------------- private static void dispResultSet (ResultSet rs, PrintWriter out) throws SQLException { int i; String d; // Get the ResultSetMetaData. This will be used for // the column headings ResultSetMetaData rsmd = rs.getMetaData (); // Get the number of columns in the result set int numCols = rsmd.getColumnCount (); // Display column headings out.println(""); out.println(""); for (i=1; i<=numCols; i++) { out.print(""); for (i=1; i<=numCols; i++) { out.print("
"); out.print(rsmd.getColumnLabel(i)); } // Display data, fetching until end of the result set while (rs.next ()) { // Loop through each column, getting the // column data and displaying out.println("
"); d = rs.getString(i); if ( d == null ) { out.print(" "); } else { out.print(d); } } // Fetch the next result set row } out.println("
"); } }