JDBC  «Prev  Next»

JBC Connection - Exercise

Create a connection

Objective: Create a connection using JDBC.

Background/overview


You will need to write code to create a connection and then a statement. A skeleton version of this class is provided for you to modify in the Brazil Hospital project that you downloaded.

Instructions

This is your opportunity to use the information you have learned. Since the focus of this chapter is on how to connect to a database, we will work with one that already exists. Normally, a new project such as the Brazil Hospital project would start by creating tables and then adding data. In this exercise, you will temporarily detour around that creation step. You will return to it in a later module. (Please note: the SQL statements that were used to create and populate this initial table are in the file CreateHospitalTable.ddl if you wish to review them. The steps to complete the exercise are listed below:

1. Load the driver.
  1. Your first task is to install and load the JDBC driver. Open your text editor, locate and modify the program, Exercise1, below the comment that begins "//<<Modification 1>>..."
  2. From this information, the Connection class derives everything it needs to create a connection to the database. Since the code that selects the driver name is already written for you, all you have to do is to load a class for the driver name and instantiate that class.

  3. Remember, a class is created by using Class.forName("MyClassName");


2. Build and return the URL.

Your second task is to specify the appropriate URL statement for the Cloudscape database. Modify the program the comment labeled "//<<Modification 2>>..." The URL consists of the protocol and the location of the database directory.

3. Compile the example.

To compile the program: Open a command prompt session, go to the JDBCCourse directory, and run the following command:

c:> javac Exercise1.java

  1. Remember to run the "setjv102env.bat" before compiling the example.
  2. You may run into errors when you try to compile a JDBC program. Here are two you may see that relate to creating a connection.
  3. "Driver not found" error. This could happen because the driver is not registered with the DriverManager--which may be because the JVM you use doesn't instantiate the driver on the call of the Class.forName() method. Try calling the Class.forName().newInstance() method to avoid the problem. If this solves the problem, continue to use this method when you use the forName() method in the remaining examples.

  4. "Class not found" error. This likely happens if the JVM cannot locate the driver in your classpath. The setjv102env.bat file should set the CLASSPATH to include the Cloudscape driver, so be sure to run that batch script before you attempt a compile. The "cloudscape.jar" file should be in your classpath. To check this, run the command "set CLASSPATH" from the command line, and you should see that file in the collection listed.

    Don't forget to check the spelling and syntax of the statements you used. It can be very frustrating to spend a lot of time tracking down errors that are caused by something as simple as transposed letters or missing punctuation.

  5. Continue on to the next step in order to test your work.


4. Run the Example1 class.

To run this example, use the following commands:

c:> <projecthome>\scripts\javaenv
c:> java com.projava.database.CloudscapeConnection

Now do the following.

  1. Insert the argument you supplied for the method that created the connection.
  2. Insert the argument you provided for the method that executed an SQL statement.
  3. Insert a copy of the output returned by running your program.

Hints

Follow the instructions very closely. Review your work carefully before proceeding to the next section.