<%@ page import="java.util.*" import="java.sql.*" %> <%@ page session="false" %> <%! // We can define methods and global variables for the page. // In particular, the jspInit method overrides the init // method in the Servlet class, allowing us to define actions // to take when the page is loaded for the first time // (in this case, loading the database driver). public void jspInit() { System.out.println("Loading driver"); try { // Register the driver (or the bridge). Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (ClassNotFoundException ex) { System.out.println("COULD NOT LOAD DRIVER!!!"); } } public String buildQuery(String who) { String queryString = "SELECT Purchases.Item, Purchases.Quantity, Purchases.When, Stock.Price " + "FROM Purchases, Stock " + "WHERE Purchases.Name = '" + who + "' " + "AND Purchases.Item = Stock.Item"; return queryString; } public String getDate(java.sql.Date date) { GregorianCalendar gc = new GregorianCalendar(); gc.setTime(date); int day = gc.get(Calendar.DAY_OF_MONTH); int month = gc.get(Calendar.MONTH) + 1; int year = gc.get(Calendar.YEAR); String dateString = "" + month + "/" + day + "/" + year; return dateString; } double total; %> ACME Orders Page <% // Make the connection. String database = config.getInitParameter("database"); Connection con = DriverManager.getConnection("jdbc:odbc:ACME"); // Get the customer name from the request String who = (request.getParameter("customer")).trim(); Statement s = con.createStatement(); ResultSet rs = s.executeQuery(buildQuery(who)); %>

Orders for <%= who %>: <% total = 0; while (rs.next()) { String item = rs.getString("Item"); int quantity = rs.getInt("Quantity"); double price = rs.getDouble("Price"); total = total + price; String dateString = getDate(rs.getDate("When")); // Write as a new table row out.println(""); out.println(""); out.println(""); out.println(""); } rs.close(); s.close(); con.close(); %>
Item Purchased Date Purchased Number Purchased Price per Item
" + item + "" + dateString + "" + quantity + "$" + price + "
Total Purchases: $<%= total %>