Using JDBC to Create Database Objects
By James W. Cooper2003-05-24
Writing JDBC Code to Access Databases
Now we are going to start looking into how you write the Java programs themselves to access databases. The database we are going to use is a Microsoft Access database called groceries.mdb. The data in this database consist of the prices for some common commodities at three local grocery stores. The food table looks like this:
| FoodKey | FoodName |
| 1 | Apples |
| 2 | Oranges |
| 3 | Hamburger |
| 4 | Butter |
| 5 | Milk |
| 6 | Cola |
| 7 | Green beans |
The store table like this:
| StoreKey | StoreName |
| 1 | Stop and Shop |
| 2 | Village Market |
| 3 | Waldbaum's |
And the grocery-store pricing table simply consists of keys from these tables followed by prices:
| FSKey | StoreKey | FoodKey | Price |
| 1 | 1 | 1 | $0.27 |
| 2 | 2 | 1 | $0.29 |
| 3 | 3 | 1 | $0.33 |
| 4 | 1 | 2 | $0.36 |
| 5 | 2 | 2 | $0.29 |
| 6 | 3 | 2 | $0.47 |
| 7 | 1 | 3 | $1.98 |
| 8 | 2 | 3 | $2.45 |
| 9 | 3 | 3 | $2.29 |
| 10 | 1 | 4 | $2.39 |
| 11 | 2 | 4 | $2.99 |
| 12 | 3 | 4 | $3.29 |
| 13 | 1 | 5 | $1.98 |
| 14 | 2 | 5 | $1.79 |
| 15 | 3 | 5 | $1.89 |
| 16 | 1 | 6 | $2.65 |
| 17 | 2 | 6 | $3.79 |
| 18 | 3 | 6 | $2.99 |
| 19 | 1 | 7 | $2.29 |
| 20 | 2 | 7 | $2.19 |
| 21 | 3 | 7 | $1.99 |
Tutorial Pages:
» What is a Database?
» Getting Data out of Databases
» Kinds of Databases
» ODBC
» What Is JDBC?
» Installing and Using JDBC
» Types of JDBC Drivers
» Two-Tier and Three-Tier Models
» Writing JDBC Code to Access Databases
» Registering Your Database with ODBC
» Connecting to a Database
» Accessing the Database
» The ResultSet
» ResultSetMetaData
» DatabaseMetaData
» Getting Information on Tables
» Executing SQL Queries, Printing out ResultSets, A Simple JDBC Program
» Building Higher Level JDBC Objects
» Building a Database Object, A Visual Database Program
» Executing a Query
» The Query Result Dialog
» Example Files
» Summary
First published by IBM DeveloperWorks
