About this tutorial
If you're interested in the various approaches to building Web applications that access a database through JDBC, then this tutorial is for you. In this hands-on guide, you'll learn the fundamentals of this process through three separate techniques: a servlet approach, a JavaServer Pages (JSP) page approach, and combined JSP, JavaBeans, and servlet approach (also known as Model Two).
The example code in this tutorial has been developed to work with the DB2 Universal Database 7.2 (see JDBC and database resources for more details on DB2), but modifying the code to work with other databases is trivial due to the use of a
This tutorial assumes you work comfortably with the Java programming language and does not delve into the particulars of servlets or JSP technology (see Resources for further reading on these subjects). The techniques presented, however, build on the skills developed in Managing database connections with JDBC and Advanced database operations using JDBC, and we recommend that you complete these tutorials prior to starting this one.
The example code snippets used throughout this tutorial are based on a fictitious message board application. It allows multiple authors to post messages and incorporates a searchable message digest that summarizes the status of the message board. If you plan on working through the examples in this tutorial, we recommend that you complete the Advanced database operations with JDBC tutorial first; doing so ensures your environment is correctly installed and configured.
The simplified version of the message board application discussed in this tutorial has three distinct entities:
- A message class
- An author class
- A digest class
Each of these entities has its own table in the database. The start page for the Web application displays the complete message digest. From the start page, a user is able to click on a message ID to display the associated message. Likewise, from a given message page, the user can click on a user ID to display the user profile (a picture in this case).
Although reading the examples helps you understand the concepts in this tutorial, most people learn better by actually trying out the examples for themselves. The following tools must be installed and working correctly before working through the examples:
- A text editor: Java source files are simply text, so to create and read them, only a text editor is required. A Java IDE can also be used, but sometimes the IDE hides too many of the details.
- A Java development environment, such as the Java 2 SDK, which is available at
The Java 2 SDK, Standard Edition version 1.4, includes the JDBC standard extensions as well as JNDI (Java Naming and Directory Interface), both of which are necessary for some of the examples in this tutorial.
- An SQL-compliant database: The examples in this tutorial have been tested with DB2 running on a Windows 2000 server, but because they are written using a
DataSourceobject, they should easily convert to other databases, including Oracle and Microsoft SQL Server. Summary contains links to more information on both JDBC and databases.
- A JDBC driver: Because the JDBC API is predominantly composed of interfaces,
an actual JDBC-driver implementation must be obtained to make the examples
in this tutorial work. The examples use advanced JDBC functionality,
and, therefore, they require an advanced JDBC driver. Most database and JDBC driver
vendors supply an evaluation version of a particular JDBC driver, including IBM, which
provides a good JDBC driver for DB2.
- A JSP page container: This type of container is required to build a JSP-based Web application. The reference implementation for both the servlet and JSP technology specification is the Apache Software Foundation's Jakarta Tomcat server, which is freely available, and is used throughout this tutorial.
- The message board tables from the tutorial "Advanced database operations with JDBC": These tables were created and populated for use in a fictitious message board application. This tutorial builds on that application and requires those tables and data to properly function. The source code from this tutorial can be downloaded from Resources.