Java Reference
In-Depth Information
relational databases, but also from spreadsheets and fl at fi les—i.e., from just about
any data source. With JDBC 2.0, which appeared in J2SE 1.4, extra functionality
was introduced with the additional package javax.sql . Probably the most notable
feature of this version was the introduction of the DataSource interface, which now
provides the preferred method of making a connection to a database, This is due to
the fact that a DataSource object has properties that can be modifi ed. Thus, for
example, if the data source is moved to a different server, the property for the server
can be changed without requiring the code accessing the data source to be changed.
In spite of the fact that the use of a DataSource object is the offi cial preferred
method of making connection to a database, the old method of making a connection,
which involves the use of the DriverManager class, is still quite commonly used.
Both of these techniques will be covered in this chapter.
The latest version of the JDBC API (at the time of writing) is JDBC 4.1, which
is included with Java SE 7, and most drivers nowadays adhere to the JDBC 4
standard, which has been included in the Java installation since Java SE 6 (launched
in December of 2006). However, users of earlier versions can expect their code
to continue to work under Java SE 7, since one of the guiding design principles of
each new version has been that of maintaining compatibility with existing appli-
cations and drivers.
In the examples that follow in the next two sections, a simple MS Access data-
base will be used for purposes of illustration, which means that the inbuilt JDBC-
ODBC bridge driver can be employed (even though, as noted at the end of 7.1, this
may not be the best strategy in many commercial applications). Convenient though
this may be, in view of the widespread use and availability of MS Access and the
inclusion of the JDBC-ODBC bridge driver in the Java installation, it does intro-
duce a couple of complications: (i) we have to create an ODBC Data Source and
(ii) we need to use the 32-bit version of Java, even though there is now a 64-bit ver-
sion. The reason for the latter is that, although a 64-bit version of MS Access does
now exist, even Microsoft is recommending that the 32-bit version be installed for
the time being: “For these reasons, we recommend running 32-bit Offi ce 2010 even
on 64-bit Windows operating systems for better compatibility…When the 64-bit
ecosystem for Offi ce is more mature, you'll be able to easily migrate to 64-bit
Offi ce” [ ce2010/archive/2010/02/23/understanding-
64- bit-offi ce.aspx ] .
The next section describes the process required to create an ODBC Data Source,
with the two sections after that describing the steps required to make connection to
the database and to retrieve or manipulate the contents of the database. Apart from
creation of the ODBC Data Source, all steps are applicable to any type of relational
database. Before starting the next section, though, it is worth mentioning that the
reader who wishes to experiment with other databases (such as Oracle or MySQL)
will probably fi nd it necessary to place the appropriate JDBC driver within folder
< Java_Home > \jre\lib\ext.
As of Java SE 6, Java also has its own inbuilt database, known as either Java DB
or Apache Derby . Use of this database will also be covered in a later section
Search WWH ::

Custom Search