Java Reference
In-Depth Information
If you are accessing one type of database, such as Oracle, Sybase, or IBM, the
preferred driver type is 4. If your Java application is accessing multiple types
of databases at the same time, type 3 is the preferred driver. Type 2 drivers are
useful in situations where a type 3 or type 4 driver is not available yet for your
database. The type 1 driver is not considered a deployment-level driver and is
typically used for development and testing purposes only.
The J2SE comes with the following type 1 bridge driver:
sun.jdbc.odbc.JdbcOdbcDriver
This is the driver we will use for the examples in the course because it comes
with the J2SE API, which means that you have it on your computer.
If you are using a version of the Windows operating system, you can find
detailed instructions on this topic's Web site showing how to create a
database using Microsoft Access. More importantly, you will find step-by-
step instructions on how to create a Data Source Name for your database
so that your Java programs can connect to your Access database. See the
Introduction for the site's URL.
Connecting to a Database
After you have a data source name created, there are two ways to establish a
connection to the database using JDBC. (See the Web site's URL provided in
the Introduction for instructions on how to do this for Windows and Microsoft
Access.)
Use the static method getConnection() in the java.sql.DriverManager
class, which takes in a URL representing the data source name.
■■
Use JNDI (Java Naming and Directory Interface) to look up the data
source name, which is returned as a javax.sql.DataSource object, and
then use the getConnection() method of the DataSource class.
■■
The Java documentation states that the technique using the DataSource class
is preferred over the DriverManager class; however, DataSource is a newer class
(J2SE 1.4), so there is lots of existing database code out there that still uses
DriverManager, not to mention that using the DataSource class typically involves
using a third-party application such as an application server and a naming ser-
vice. If you are writing a standalone Java application, such as all of the examples
in this topic, the DriverManager class is the technique most often used.
No matter which technique you choose, they are similar in terms of coding,
so I will show both of them to you.
Search WWH ::




Custom Search