Java Reference
In-Depth Information
Example 17−4: LookupAPI.java (continued)
/**
* This program uses the database created by MakeAPIDB. It opens a connection
* to a database using the same property file used by MakeAPIDB. Then it
* queries that database in several interesting ways to obtain useful
* information about Java APIs. It can be used to look up the fully qualified
* name of a member, class, or package, or it can be used to list the members
* of a class or package.
**/
public class LookupAPI {
public static void main(String[] args) {
Connection c = null;
// JDBC connection to the database
try {
// Some default values
String target = null; // The name to look up
boolean list = false; // List members or lookup name?
String propfile = "APIDB.props"; // The file of db parameters
// Parse the command-line arguments
for(int i = 0; i < args.length; i++) {
if (args[i].equals("-l")) list = true;
else if (args[i].equals("-p")) propfile = args[++i];
else if (target != null)
throw new IllegalArgumentException("Unexpected argument: "
+ args[i]);
else target = args[i];
}
if (target == null)
throw new IllegalArgumentException("No target specified");
// Now determine the values needed to set up the database
// connection The program attempts to read a property file
// named "APIDB.props", or optionally specified with the
// -p argument. This property file may contain "driver",
// "database", "user", and "password" properties that
// specify the necessary values for connecting to the db.
// If the properties file does not exist, or does not
// contain the named properties, defaults will be used.
Properties p = new Properties(); // Empty properties
try { p.load(new FileInputStream(propfile)); } // Try to load props
catch (Exception e) {}
// Read values from Properties file
String driver = p.getProperty("driver");
String database = p.getProperty("database");
String user = p.getProperty("user", "");
String password = p.getProperty("password", "");
// The driver and database properties are mandatory
if (driver == null)
throw new IllegalArgumentException("No driver specified!");
if (database == null)
throw new IllegalArgumentException("No database specified!");
// Load the database driver
Class.forName(driver);
// And set up a connection to the specified database
c = DriverManager.getConnection(database, user, password);
Search WWH ::




Custom Search