Java Reference
In-Depth Information
Applications don't have to use databases. However, without a database, applications are very difficult and
time-consuming to use. For instance, would a payroll application be easy to use if all the employee's information had
to be entered each time the application was run? If instead all the employee information is entered into a database
once, the application can access that information at any time.
Establishing a connection to a database is a little cumbersome but, if coded wisely, only has to be specified once.
In fact, a primary goal is to hide (from the programmers) the specifics of how to access a database. Organizations try to
hide the specifics such that Java programmers have no idea what DBMS is being accessed or even where the database
is located. This “design” goal goes by many names such as “data encapsulation,” “transparency,” “information hiding,”
and/or “implementation ignorance.”
First, we will explain and demonstrate the basics for accessing a database and then show how to efficiently define
and use the necessary classes/objects.
Establishing a DB Connection
Several new classes will help establish a database connection and then access the database. However, there is one
piece of the puzzle, the driver , which does not “come with” Java. This is because the individual DBMS vendors must
supply the driver software to access their DBMS. In other words, the Java developers specify how a database driver
must work, but it is up to the DBMS provider (IBM, Oracle, Microsoft, etc.) to create and supply the software for Java
to work with their DBMS (DB2, Oracle, Access, etc.). Most DBMS providers are very eager to supply drivers because if
there is no driver, Java applications cannot use the DBMS. Of course, this means they cannot sell you their DBMS. You
can tell that the DBMS providers are very eager to supply the driver software because they provide it for free! When
downloaded, a driver is usually supplied as a ZIP or JAR file. (Microsoft, however, includes the driver for its Access
DBMS within the Windows operating system.)
To use the driver, you must identify the driver's location (on the PC) to RAD. One way to do this is to add the
driver location to the “Java Build Path.” RAD uses the Java Build Path to find classes that are not in the project.
Therefore, we need to add an external JAR to RAD's Java Build Path. For this discussion, we have assumed that
the driver (in a JAR) is on the PC's C: drive. (Microsoft Access, however, is different. The MS Access driver must be
identified when defining an Access DB as an ODBC data source in Windows.)
Once the driver is identified, the Java class must load the driver. You can think of loading the driver as
creating an instance of the driver class. However, as with the formatting classes (covered in an earlier chapter), the
programmer does not explicitly create the driver object. Instead, the Class class's forName method is used.
(Yes, there is a class called Class !) The forName method returns an object of the class type specified. For instance,
the following statement:
Class.forName("com.ibm.as400.access.AS400JDBCDriver");
returns a driver object that can be used to access a DB2 database on a Power System (an IBM midrange computer).
Next, a Connection object must be created. Once again, you do not directly create this object. Instead, you use
the DriverManager class's getConnection method. The getConnection method requires the following information to
create the connection:
The
protocol (type of driver) used to access the database.
The
location of the database . This will include at least the IP address or URL where the
database resides. The driver type dictates what other location information is required.
Driver
properties . Most drivers require a user ID and password. Optional properties can
be specified to control other aspects of the database interface.
A Statement object then needs to be created. Statement objects have execute methods that allow the programmer
to run SQL commands that insert, retrieve, delete, or update data in the database. Again, the programmer does not
explicitly create the needed object. Instead, the Connection object's createStatement method is used.
 
Search WWH ::




Custom Search