July 9, 2020
Hot Topics:

Connecting to a Database with JDBC

  • By Matt Weisfeld
  • Send Email »
  • More Articles »

This series, The Object-Oriented Thought Process, is intended for someone just learning an object-oriented language and who wants to understand the basic concepts before jumping into the code, or someone who wants to understand the infrastructure behind an object-oriented language he or she is already using. These concepts are part of the foundation that any programmer will need to make the paradigm shift from procedural programming to object-oriented programming.

Click here to start at the beginning of the series.

In keeping with the code examples used in the previous articles, Java will be the language used to implement the concepts in code. One of the reasons that I like to use Java is because you can download the Java compiler for personal use at the Sun Microsystems Web site http://java.sun.com/. You can download the J2SE 1.4.2 SDK (software development kit) to compile and execute these applications and I will provide the code listings for all examples in this article. I have the SDK 1.4.0 loaded on my machine. I will also provide figures and the output (when appropriate) for these examples. See the previous articles in this series for detailed descriptions for compiling and running all the code examples.

In the two previous articles in this series, I covered the topics of object signatures and object serialization. In this article, you will explore the ways to connect your Java applications to a database using Java Database Connectivity (JDBC). In this specific case, you will connect to a Microsoft Access database and utilize several aspects of JDBC functionality.

A JDBC Tutorial

For this installment, I am taking a somewhat different approach from the previous articles by using a tutorial format to illustrate the basic concepts of JDBC. The reason for this is that, to utilize JDBC functionality, you must interact directly with your operating system; thus, there is a certain amount of system configuration that you must complete. To demonstrate the system configuration aspects, the liberal use of screen shots is very helpful. I actually use this tutorial technique in the classroom when teaching an Object Technology class that I offer. It is presented in HTML format just like the content of this article. In this way, the students can follow the examples and apply the concepts directly to their computer.

When this tutorial is complete, you will have a working application that uses JDBC to connect directly to a database. As stated earlier, the application you develop here will present code to interact with a Microsoft Access database. In this installment, I will demonstrate the basic techniques necessary to get JDBC up and running and communicating with the database. This includes the code for the actual database connection, doing simple queries, and so forth. In next month's article, you will explore JDBC even further by taking advantage of functionality such as table creation, record updates, and much more.

Creating the Database & DSN Connection

Before I launch into the actual application code, you first should create the database you will utilize in this tutorial. There are really no constraints to which database implementation you can use. Even though you use Microsoft Windows XP and Access in this example, you could choose any operating system and/or database that supports Java technology. In my class, students have used various platforms, including a Linux/MySQL solution. The primary difference when using another platform solution is in the way in which the driver and operating system connections are completed. The Java code should not vary except in the string that loads the driver—which you will see later on.

The application will work even is you don't create the database yourself. You could, for example, simply place the Access database file on your system and let the code connect to it. However, you will create the Access database so you can see how the process works from start to finish. If you are using another database system, you can modify the instructions in the next section to conform to the system that you are using.

Creating the Microsoft Access Database

The design of your database is quite simple and represents a small baseball equipment operation. The database will contain four fields: ID, Name, Price, and Quantity, as illustrated in Table 1.

ID Name Price Quantity
1 Baseball $2.00 200
2 Bat $20.00 20
3 Glove $80.00 0
4 Catchers Glove $100.00 1

Table 1

All you need to do is create the Access database and enter the data. To do this, launch Microsoft Access and create a blank database called purchaseOrder.mdb. The initial screen is shown in Figure 1.

Figure 1

After you specify the name of the database as purchaseOrder.mdb, you then need to create the single table that you will utilize in this example. To accomplish this, you can use the Create table by entering data option as seen in Figure 2.

Figure 2

After you enter all of the data in the table you designed, your database should look like Figure 3.

Figure 3

Making the DSN Connection

Now that the database is complete, there is one more step you need to take to ensure the Java application you develop can make a connection to it. This step is highly dependent on the platform that you are using. In this example, you need to make a Data Source name (DSN) connection to the database. This is the mechanism that allows the application to connect to the database itself.

To accomplish this, you first need to bring up the Control Panel as indicated in Figure 4 and click on the Administrative Tools icon.

Figure 4

Once you get to the Administrative Tools dialog box, you then click on the Data Sources icon as seen in Figure 5.

Figure 5

Page 1 of 3

This article was originally published on December 16, 2005

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date