Java Reference
In-Depth Information
Accessing a Database
Example 17-1 shows a program that connects to a database and then loops,
prompting the user for a SQL statement, sending that statement to the database,
and displaying the results. It demonstrates the four most important techniques for
JDBC programming: registering a database driver, using the DriverManager class to
obtain a Connection object that represents a database connection, sending a SQL
statement to the database using the Statement object, and retrieving the results of
a query with a ResultSet object. Before we look at the specifics of the ExecuteSQL
program, let's examine these basic techniques.
One of the interesting things about the java.sql package is that its most important
members, such as Connection , Statement , and ResultSet , are interfaces instead of
classes. The whole point of JDBC is to hide the specifics of accessing particular
kinds of database systems and these interfaces make that possible. A JDBC driver
is a set of classes that implement the interfaces for a particular database system;
different database systems require different drivers. As an application programmer,
you don't have to worry about the implementation of these underlying classes. All
you have to worry about is writing code that uses the methods defined by the var-
ious interfaces.
The DriverManager class is responsible for keeping track of all the JDBC drivers
that are available on a system. So the first task of a JDBC program is to register an
appropriate driver for the type of database being used. By convention, JDBC
driver classes register themselves with the DriverManager when they are first
loaded, so, in practice, all you have to do is load the driver class, allowing it to
register itself. The Class.forName() method provides one easy way of doing this.
This method takes a String argument that specifies a class name, so it's simple to
pass the driver name to the program on the command line, instead of hardcoding
the driver class into your program. Note that this step simply loads a driver and
registers it with the DriverManager ; it doesn't specify that the program actually use
that driver. If a program needs to use multiple databases, it can load multiple
driver classes in this step. The driver selection step comes next, when the program
actually connects to a database.
After the required driver is loaded (and has registered itself), a JDBC program can
connect to the database by calling DriverManager.getConnection() . You specify
the database to connect to with a jdbc: URL. This URL has this general syntax:
jdbc: subprotocol :// host : port / databasename
The subprotocol of the URL identifies the particular database system that is being
used. The DriverManager class uses that part of the URL to select an appropriate
JDBC driver from the list of drivers that have been registered. If the DriverManager
can't find a JDBC driver for the database, it throws a SQLException .
I used the MySQL database while developing the examples in this chapter, so I
had to use a URL like the following to connect to my database:
jdbc:mysql://dbserver.mydomain.com:1234/mydb
Search WWH ::




Custom Search