Hooking Up with IBM DB2 Universal Database Version 8 Using Java
The relational database serves as a location to persist data for many applications. Java programmers commonly use JDBC to interact with backend databases. This article shows you how to set up your environment to write Java programs that interact with IBM DB2 Universal Database Version 8. You will notice that there are different approaches to connecting to DB2 that require different drivers, software, and syntax. In this article, we'll shed light on the subject so you can evaluate which approach is appropriate for you getting your Java program "hooked up" to DB2.
DB2 Universal Database Version 8.1 fully supports the JDBC v2.1 specification. It also supports portions of the JDBC 3.0 specification. Usage on the new JDBC 3.0 features of the driver requires a JDK 1.4 environment.
DB2 Version 8 provides support for four different JDBC driver architectures, named Type 1, Type 2, Type 3, and Type 4. In this article, we'll focus on the Type 2 and Type 4 drivers because they are the drivers that you will most likely use to enable your Java applications to talk to DB2.
Something to Hook Up to
Because we plan to show how to use Java to interact with a database, we need a database to play with. Let's dedicate some time to create one here. Suppose that you have a user on your DB2 server with the name of db2admin and a password of db2admin. Let's create a small database on our database server, associating a name to a phone number. We'll call our database phonedb. Using the DB2 command line processor, enter the following command:
db2 => create database phonedb
Next, we connect to the database:
db2 => connect to phonedb user db2admin using db2admin
We create our table using this command:
db2 => create table phonenumbers (name varchar(60) not null primary key, phonenumber varchar(13) not null)
Finally, let's insert a couple of records:
db2 => insert into phonenumbers values('Jack Price','(888)888-8888') db2 => insert into phonenumbers values('David Jones',(555)555-5555')