table of contents
CSAJSP/2

Source Code Archive

Core Servlets and JavaServer Pages

Second Edition

Volume 1: Core Technologies


Chapter 17: Accessing Databases with JDBC

Complete source code for every example from this chapter, available free for unrestricted use. Live links to all URLs cited in the chapter. More information. To view source code for other chapters in this book, mouse over the table of contents bar on the left.

Source Code from Chapter

Note: right-click or shift-click on the links to download the associated file.

  • NorthwindTest.java. Standalone application that connects to the MicroSoft Access sample Northwind database, issues a simple SQL query to the employee table, and prints the results.
  • NorthwindServlet.java. Servlet that connects to a database and presents the results from the query in an HTML table. The driver, URL, username, password, and query are taken from form input parameters.
  • NorthwindForm.html. HTML form that supplies the request parameters used by the NorthwindServlet.
  • DriverUtilities.java. Some simple utilities for building JDBC connections to databases from different vendors. The drivers are specific to our local setup, but you can either modify the loadDrivers method and recompile or use DriverUtilities2 to load the driver information from an XML file.
  • DriverUtilities2.java. Extends DriverUtilities to support the loading of driver information from an XML file (default is drivers.xml).
  • drivers.xml. XML file that represents driver information. Used by the DriverUtilities2 class. Install this in the WEB-INF directory.
  • DriverInfoBean.java. Class that represents driver information for a vendor. In the coreservlets.beans package.
  • ConnectionInfoBean.java. Class that stores information to create a JDBC connection. Information includes:
    • connection name
    • description of the connection
    • driver classname
    • URL to connect to the host
    • username
    • password
  • In the coreservlets.beans package.
  • PreparedStatements.java. An example to test the timing differences resulting from repeated raw queries vs. repeated calls to prepared statements.
  • CallableStatements.java. An example that executes the Oracle stored procedure discount. Specifically, the price of all compositions by Mozart in the music table are discounted by 10 percent. To create the stored procedure, specify a command-line argument of create or use the discount.sql definition directly.
  • discount.sql. PL/SQL function for Oracle.
  • Transactions.java. An example to demonstrate submission of a block of SQL statements as a single transaction.
  • TransactionBean.java. Bean for performing JDBC transactions. After specifying the connection, submit a block of SQL statements as a single transaction by calling execute. If an SQLException occurs, any prior statements are automatically rolled back. In the coreservlets.beans package.
  • Music.jdo. XML file that maps the Music class to a table in the database.
  • Music.java. Class corresponding to a record in a database. A Music object/record provides information about a concerto that is available for purchase and defines fields for the ID, composer, concerto, items available, and sales price. In the coreservlets.jdo package.
  • PopulateMusicTable.java. Class that populates database with music records by using JDO. In the coreservlets.jdo package.
  • jdo.properties. Configuration information for Kodo JDO Framework and MySQL database. Goes in WEB-INF/classes.
  • jdbc.war. Web ARchive (WAR) file for the examples from this chapter.

URLs Referenced in Chapter

More Information

Java

JSF (JavaServer Faces)

Servlets & JSP
Ajax, GWT, & JavaScript

Spring, Hibernate, & JPA

Struts