Java Reference
In-Depth Information
position it afterLast() , then getRow() to get the size, then position it back
to beforeFirst() to be ready to read.
15.6
U PDATES , I NSERTS , D ELETES
Not every action on a database returns a ResultSet . Operations that create
the tables in a database, or those that modify, insert, or delete rows of data don't
return rows of values. For those sorts of SQL statements, we don't call
executeQuery() —we call executeUpdate() instead. It returns an int giving
the number of rows affected by the execution. In the case of a CREATE TABLE
operation, it simply returns 0.
15.7
R EVIEW
Connecting a Java application to a database is a key step in real applications.
The mechanisms for doing that are varied and can be complicated. We've
picked a single approach for connecting, to keep it simple and to highlight the
similarities and differences between two different database implementations.
The rest of the conversation with a database depends as much on your
SQL skills as on Java skills. Java will take strings of SQL, which can be param-
eterized, and, via JDBC calls, send them to be executed by the database. The
results are like iterators; they can be retrieved, row after row, for further process-
ing. Updates, inserts, and deletes are also easily done, with a few simple calls to
process the SQL.
15.8
W HAT Y OU S TILL D ON ' T K NOW
We've skipped over lots of topics to keep this simple. There are a variety of
ways to connect to a database that we haven't covered; the most important one
may be the use of a DataSource instead of a DriverManager . As of Java 1.4.1,
the DataSource is the preferred means of making connections to your
database. While it makes the code more portable (e.g., if you're planning on
moving around, changing databases and/or database servers), it is more compli-
cated to set up—there are more “moving parts” to get right. If you already have
a Java Naming and Directory Interface (JNDI) service running, thought, it's
very straightforward (see page 254 and the following pages of Gallardo).
Search WWH ::




Custom Search