Java Reference
In-Depth Information
This code throws three checked exceptions that must be caught:
ClassNotFound
Exception, InstantiationException
, and
IllegalAccessException
. For simplicity,
we catch only the superclass
Exception
in the code shown in Display 19.12.
Once the database drivers are loaded, we can connect to the database. This is done
by passing a
connection string
to the
DriverManager.getConnection
method. The
connection string specifies the protocol, database name, and other parameters, such as
whether or not a new database should be created. For example, to connect to and create
a database named
BookDatabase
using Derby, we would use
connection
string
Connection conn =
null
;
conn = DriverManager.getConnection(
"jdbc:derby:BookDatabase; create = true");
This creates a subdirectory named
BookDatabase
in the active working directory that
will contain the database, files. You could specify a pathname in front of
BookDatabase
if you want to create the database somewhere else on the file system. If you ever wish
to delete the database, then simply delete the
BookDatabase
directory. Note that if
the database already exists, then the attribute
create=true
will not delete the existing
database but will instead connect to the existing database.
Additional parameters are specified in the command string by separating them with
semicolons. For example, if the database requires a username and password, then the
connection string would look like this:
conn = DriverManager.getConnection
("jdbc:derby:BookDatabase;create=true;user=username;" +
"password = pass");
When we are finished accessing the database, invoke the
close( )
method to close
the connection. The
DriverManager.getConnection( )
method requires that
SQLException
be caught, so this code should be placed inside an appropriate try/
catch block.
SQL commands or queries can be issued to JDBC once the database connection
is established. First, a
Statement
object must be constructed and then invoked by
calling the
execute
or
executeQuery
method with a SQL string as its argument.
The
execute
method can be used to execute any SQL statement, but it is generally
used for SQL commands where return values are not needed or ignored (e.g., creating
a new table or deleting a row). It returns
true
if the command results in a set of data
and
false
if there is no result or an update count. The
executeQuery
method is used
with a SQL query that is expected to return some rows from the database. It returns a
ResultSet
object that contains the data produced by the query. These methods throw
SQLException
if there is a database error. Display 19.12 illustrates how to create a
new table and insert three rows using the
CREATE TABLE
and
INSERT
commands. The
result is identical to the
names
table in Display 19.10. Note that the program should
only be run once. If you attempt to run it a second time, the program will throw an
exception when executing the
CREATE TABLE
command, because it is invalid to create
a new table that matches an existing name.