Previous Table of Contents Next


CHAPTER 3
Basic JDBC Programming

This chapter covers the basic use of JDBC. The goal of this chapter is to enable you to create database connections and access database values via JDBC. This chapter describes the steps to connect to a data source and query it using the JDBC API. The data source example used in this chapter is a Microsoft Access database called INV.mdb, found on the CD–ROM that accompanies this book. This is a small inventory database with four user tables. The Access database was chosen because it is common and can be accessed through the JDBC–ODBC driver. This means you don’t need a special driver for the database, because Access comes with an ODBC driver.


NOTE:  If you do not have Microsoft Access for the examples in this chapter, the CD–ROM included with this book includes a build script called Inv.script that can be used and modified to create this schema in most relational databases.

About the Sample Database

The sample INV database used in the next few examples is a simple inventory database. The tables involved are described in Tables 3.1 through 3.4. Figure 3.1 shows the relationships among the tables. The examples in this chapter perform various queries on these tables, such as looking up all the vendors that sell beverages. The tables include the following :

Table 3.1 Categories Schema

FIELD TYPE NOTES

CategoryID AutoNumber (integer) Primary key

CategoryName Text

Categories. A list of inventory types, such as Beverage, Produce, or Seafood.
Inventory. A list of inventory items such as Chef Anton’s Gumbo Mix and associated information such as unit price.
Supplier. A list of vendors that sell the product in the inventory list.
ProductSuppliers. A list of inventory items provided by each supplier.


NOTE:  For this chapter, it is important that you understand SQL and have access to a relational database. If you do not have Microsoft Access, make sure you modify the INV.script so that it creates the appropriate tables on your database. In any event, you will need to set up an ODBC data source called Inventory that refers to your database. Check the documentation that comes with your ODBC driver for information on how to create an ODBC data source. This relationship among the ODBC driver, JDBC, and the ODBC data source should become more clear as you examine the code used to make a database connection. For now, keep in mind that to run the examples, you need a database. If you can’t use the one used here, you will need to alter the code provided on the CD–ROM to make it work with this database.
Table 3.2 Inventory Schema

FIELD TYPE NOTES

ProductID AutoNumber (integer) Primary key

ProductName Text

ProductDescription Text

CategoryID Number (integer) Foreign key

Price Currency

ReorderLevel Number (integer)

Discontinued Yes/No (Boolean)

LeadTime Text

Quantity Number

Table 3.3 Supplier Schema

FIELD TYPE NOTES

SupplierID Text Primary key

SupplierName Text

ContactName Text

ContactTitle Text

Address Text

City Text

PostalCode Text

StateOrProvince Text

Country Text

PhoneNumber Text

FaxNumber Text

PaymentTerms Text

EmailAddress Text

Notes Memo


Previous Table of Contents Next