Java Reference
In-Depth Information
Example 17−3: MakeAPIDB.java (continued)
insertmember.setBoolean(3, false);
// It is not a field
insertmember.executeUpdate();
// Insert into db
}
// Do the same thing for the non-private fields of the class
Field[] fields = c.getDeclaredFields(); // Get a list of fields
for(int i = 0; i < fields.length; i++) { // For each non-private
if (Modifier.isPrivate(fields[i].getModifiers())) continue;
insertmember.setInt(1, classId); // Set the class id
insertmember.setString(2, fields[i].getName()); // Set field name
insertmember.setBoolean(3, true);
// It is a field
insertmember.executeUpdate();
// Insert the record
}
}
}
Using the API Database
Example 17-4 displays a program, LookupAPI , that uses the database built by the
MakeAPIDB program of Example 17-3. LookupAPI behaves as follows:
When invoked with the name of a class member, it lists the full name (includ-
ing package and class) of each field and/or method that has that name.
When run with the name of a class, it lists the full name of every class in any
package that has that name.
When called with a portion of a package name, it lists the names of all the
packages that contain that string.
When invoked with the -l option and a class name, it lists every member of
every class that has that name.
When run with the -l option and a portion of a package name, it lists all the
classes and interfaces in any package that matches that string.
LookupAPI reads the same APIDB.pr ops property file MakeAPIDB does. Or, alterna-
tively, it reads a property file specified on the command line following a -p flag.
Using the database connection parameters in the property file, the program con-
nects to a database and executes the necessary SQL queries to return the desired
information. Note that it calls the setReadOnly() method of the Connection object.
Doing this provides a hint that the program performs only queries and doesn't
modify the database in any way. For some database systems, this may improve
efficiency. Other than the setReadOnly() method, this example doesn't introduce
any new JDBC features. It simply serves as a real-world application of a database
and demonstrates some of the powerful database queries that can be expressed
using SQL.
Example 17−4: LookupAPI.java
package com.davidflanagan.examples.sql;
import java.sql.*;
import java.io.FileInputStream;
import java.util.Properties;
Search WWH ::




Custom Search