Java Reference
In-Depth Information
LISTING 18.3
Continued
108: if (arguments[0].equals(“read”)) {
109: prez.readDatabase();
110: }
111: }
112: }
Using this application with another database and driver would require changes to lines
16, 19, 75, and 77.
Java DB requires a system property, derby.system.home , to be set to the location of the
root folder where its databases are located. If this folder does not exist, Java DB will
create it.
The Java DB JDBC driver can be loaded with the following statement:
Class.forName(“org.apache.derby.jdbc.EmbeddedDriver”);
The Presidents application is split into the createDatabase() and readDatabase()
methods whose functions are self-explanatory.
Database creation employs the following database connection string for the
DriverManager.getConnection( String ) method in line 21:
jdbc:derby:presidents;create=true
This string follows the form “jdbc:derby:” followed by the database name, a semicolon,
and the parameter “create=true”, which causes the database to be created if necessary.
This string can include user and password parameters for a database that requires logon:
jdbc:derby:presidents;user=dbuser;password=tortuga;create=true
Making a connection to read from the database in line 79 is simpler:
jdbc:derby:presidents
After you've made a successful connection to Java DB, reading and writing database
records over JDBC follows the same process employed earlier today with JDBC-ODBC.
SQL statements are written to create a database table, insert records into the table, and
read those records.
The SQL employed by Java DB has different record types than Access, MySQL, and
other databases.
Run the Presidents application the first time with “create” as the only argument to cre-
ate the new database:
java Presidents create
 
Search WWH ::




Custom Search