Java Reference
In-Depth Information
Example 17−3: MakeAPIDB.java
package com.davidflanagan.examples.sql;
import java.sql.*;
import java.lang.reflect.*;
import java.io.*;
import java.util.*;
/**
* This class is a standalone program that reads a list of classes and
* builds a database of packages, classes, and class fields and methods.
**/
public class MakeAPIDB {
public static void main(String args[]) {
Connection c = null;
// The connection to the database
try {
// Read the classes to index from a file specified by args[0]
ArrayList classnames = new ArrayList();
BufferedReader in = new BufferedReader(new FileReader(args[0]));
String name;
while((name = in.readLine()) != null) classnames.add(name);
// 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 by args[1]. This
// property file (if any) 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(args[1])); // Try to load properties
}
catch (Exception e1) {
try { p.load(new FileInputStream("APIDB.props")); }
catch (Exception e2) {}
}
// 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 driver. It registers itself with DriverManager.
Class.forName(driver);
// And set up a connection to the specified database
c = DriverManager.getConnection(database, user, password);
// Create three new tables for our data
// The package table contains a package id and a package name.
// The class table contains a class id, a package id, and a name.
// The member table contains a class id, a member name, and an bit
Search WWH ::




Custom Search