Using More Advanced JDBC Features
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 previous article, I covered the basics of connecting to a database using Java Database Connectivity (JDBC). In these examples, you will connect to a Microsoft Access database; however, you are not limited to a specific technology when using JDBC. In future articles, you will explore other databases.
To demonstrate the basics of using JDBC, you utilized an Access database that was previously created. You then proceeded to connect to the database and perform various simple queries. By using the coding techniques in these examples, you were able to verify that JDBC was performing as designed and that the physical connection to the database was made successfully.
One of the interesting exercises of the last column was to navigate through the Microsoft Windows Control Panel to properly configure the ODBC Data Source. In this column, we will explore JDBC code that will actually create a table and then perform more advanced transactions such as deleting and inserting records into those tables.
A JDBC Tutorial, Part 2
For these JDBC installments, I take a somewhat different approach from the previous articles in the series by using a tutorial format. The reason for this is that, to utilize JDBC functionality, you must interact directly with the operating system that you are working on; 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 connects directly to a database using JDBC.
Creating the Database & DSN Connection
As with last month's column, before you delve into the actual code, you must first set up the Data Source for the database. In fact, even before you do this, you must create the database itself. Previously, you used a database that was built using the functionality provided by Microsoft Access. In this column, you'll use Access to actually create the blank database and then use JDBC to do the rest.
Creating the Microsoft Access Database
To start, create a directory called JDBCApp off your root. You will use this directory structure to make the example a bit easier to understand. Once the directory is created, launch Access, create a Blank Database, and save it in this directory as seen in Figure 1.
That is all you have to do in interacting with Microsoft Access (at least the application itself). You will use Access to inspect what your application created; however, you will use JDBC to perform subsequent tasks.
Creating the DSN Connection
Now, make the DSN connection. To accomplish this with Microsoft Windows, you first need to bring up the Control Pane,l as indicated in Figure 2, and click on the Administrative Tools icon.
Once you get to the Administrative Tools dialog box, you then click on the Data Sources icon as seen in Figure 3.
Clicking on the Data Sources icon will bring you to the ODBC Data Source Administrator that will allow you to specify the name and the driver to be used by the application, as shown in Figure 4.
Once you are in the ODBC Data Source Administrator, click on the File DSN tab and then the Add button. (Note that you won't see the Business System Data Source Name until you complete the add process.) The Microsoft ODBC Access Setup dialog box appears as in Figure 5.
There are two things that must be completed in this dialog box:
- Choose a Data Source Name.
- Click on the Select button and find the business.mdb file that you previously created as shown in Figure 6. Then, click the OK button.
At this point, the DSN connection is complete and you can proceed to the second part of this tutorial, developing the code to utilize the database and the DSN connection that you created.