Java Reference
In-Depth Information
Example 17−4: LookupAPI.java (continued)
// Tell it we will not do any updates.
// This hint may improve efficiency.
c.setReadOnly(true);
// If the "-l" option was given, then list the members of
// the named package or class. Otherwise, lookup all
// matches for the specified member, class, or package.
if (list) list(c, target);
else lookup(c, target);
}
// If anything goes wrong, print the exception and a usage message. If
// a SQLException is thrown, display the state message it includes.
catch (Exception e) {
System.out.println(e);
if (e instanceof SQLException)
System.out.println(((SQLException) e).getSQLState());
System.out.println("Usage: java LookupAPI [-l] [-p <propfile>] " +
"target");
}
// Always close the DB connection when we're done with it.
finally {
try { c.close(); } catch (Exception e) {}
}
}
/**
* This method looks up all matches for the specified target string in the
* database. First, it prints the full name of any members by that name.
* Then it prints the full name of any classes by that name. Then it
* prints the name of any packages that contain the specified name
**/
public static void lookup(Connection c, String target) throws SQLException
{
// Create the Statement object we'll use to query the database
Statement s = c.createStatement();
// Go find all class members with the specified name
s.executeQuery("SELECT DISTINCT " +
"package.name, class.name, member.name, member.isField"+
" FROM package, class, member" +
" WHERE member.name='" + target + "'" +
"
AND member.classId=class.id " +
"
AND class.packageId=package.id");
// Loop through the results, and print them out (if there are any).
ResultSet r = s.getResultSet();
while(r.next()) {
String pkg = r.getString(1); // package name
String cls = r.getString(2); // class name
String member = r.getString(3); // member name
boolean isField = r.getBoolean(4); // is the member a field?
// Display this match
System.out.println(pkg + "." + cls + "." + member +
(isField?"":"()"));
}
// Now look for a class with the specified name
s.executeQuery("SELECT package.name, class.name " +
Search WWH ::




Custom Search