Java Reference
In-Depth Information
To review, the application environment must first be set up correctly. This means:
1.
Downloading the correct database driver (not necessary for MS Access)
2.
Specifying the driver
Then a Java class must do the following to access a database:
1.
Load the driver with the Class.forName method
Create a Connection object using the DriverManager.getConnection method
2.
Create a Statement object using the Connection object's createStatement method
3.
Use the Statement object's execute methods to perform SQL commands
4.
First, we will perform the generic tasks that are required regardless of the DBMS being used. Then there will be
three tutorials showing how to connect to three different types of DBMSs. The way the tutorials are written, you can
only do one of these. (If you do not have access to an Oracle database or DB2 on a Power System, do the “Set Up for
Accessing a Microsoft Access Database” tutorial. Even though Access is rarely used for commercial applications, it is
widely available and this tutorial will give you a taste of database programming.) We assume that each of these DBMSs
has a database called tntdb and a table called Employee with fields that correspond to the Employee class's properties.
Let's do it!
Tutorial: Setting up the Project
The first task is to set up the project.
1.
In the Java Perspective, click the Tutorials/src package to select it.
2.
Click File, New, and then Package.
3.
At the New Java Package window, specify c10 as the name and click the Finish button.
4.
With the folder c10 still selected, click File, New, and then Class.
5.
At the New Java Class window, specify DBAccess as the class name, make sure a main
method is created, and click the Finish button.
A new class called DBAccess is created in c10 and the source code will be displayed.
6.
Add the following source code to define a null constructor:
public DBAccess() { }
We will now add code to the main method so that we can test the class.
7.
Add the following statement to the main method.
DBAccess dba = new DBAccess();
8.
Save the source code.
 
Search WWH ::




Custom Search