Java Reference
In-Depth Information
database interprets the prepared statement and creates its template just once.
Then, when you execute the prepared statement repeatedly with different input
parameters, the database uses the template it has already created. JDBC provides
the
PreparedStatement
class to support prepared statements, but it doesn't guar-
antee that the underlying database actually takes advantage of them.
You create a
PreparedStatement
with the
prepareStatement()
method of a
Con-
nection
object, as shown in Example 17-3.
MakeAPIDB
passes a SQL statement to
prepareStatement()
, substituting
?
placeholders for the variable parameters in the
statement. Later, before the program executes the prepared statement, it binds val-
ues to these parameters using the various
set
X
()
methods (e.g.,
setInt()
and
setString()
)ofthe
PreparedStatement
object. Each
set
X
()
method takes two
arguments: a parameter index (starting with 1) and a value. Then the program calls
the
executeUpdate()
method of the
PreparedStatement
to execute the statement.
(
PreparedStatement
also provides
execute()
and
executeQuery()
methods, just
like
Statement
.)
MakeAPIDB
expects its first argument to be the name of a file that contains a list of
classes to be placed into the database. The classes should be listed one to a line;
each line must contain a fully qualified class name (i.e., it must specify both pack-
age name and class name). Such a file might contain lines like the following:
java.applet.Applet
java.applet.AppletContext
java.applet.AppletStub
...
java.util.zip.ZipOutputStream
The program reads database parameters from a
Properties
file named
APIDB.pr ops
in the current directory or from an alternate
Properties
file specified
as the second command-line argument. This
Properties
file is similar to, but not
quite the same as, the one used in conjunction with Example 17-2; it should con-
tain properties named
driver
,
database
,
user
, and
password
. On my system, the
APIDB.pr ops
file looks as follows:
# The full classname of the JDBC driver to load: this is a MySQL driver
driver=org.gjt.mm.mysql.Driver
# The URL of the mysql server (localhost) and database (apidb) to connect to
database=jdbc:mysql:///apidb
# The name of the database user account
user=david
# The password for the database user account.
# Uncomment the line below to specify a password
#password=
Note that before you run this program, you must create the database for it on your
database server. To do this, you have to follow the instructions provided by your
database vendor. You can also use an existing database, as long as it doesn't
already contain tables named
package
,
class
,or
member
.
*
*
This program performs a large number of database updates. In my tests, the MySQL server was signifi-
cantly faster than the PostgreSQL server for this program. Also, PostgreSQL 6.5 did not recognize the
standard
BIT
type used for the
member
table. If you plan to use PostgreSQL for this example, you have
to change the type of the
isField
column of that table.