Java Reference
In-Depth Information
Example 17−2: GetDBInfo.java (continued)
public static void main(String[] args) {
Connection c = null; // The JDBC connection to the database server
try {
// Look for the properties file DB.props in the same directory as
// this program. It will contain default values for the various
// parameters needed to connect to a database
Properties p = new Properties();
try { p.load(GetDBInfo.class.getResourceAsStream("DB.props")); }
catch (Exception e) {}
// Get default values from the properties file
String driver = p.getProperty("driver"); // Driver class name
String server = p.getProperty("server", ""); // JDBC URL for server
String user = p.getProperty("user", ""); // db user name
String password = p.getProperty("password", ""); // db password
// These variables don't have defaults
String database = null; // The db name (appended to server URL)
String table = null;
// The optional name of a table in the db
// Parse the command-line args to override the default values above
for(int i = 0; i < args.length; i++) {
if (args[i].equals("-d")) driver = args[++i]; //-d <driver>
else if (args[i].equals("-s")) server = args[++i];//-s <server>
else if (args[i].equals("-u")) user = args[++i]; //-u <user>
else if (args[i].equals("-p")) password = args[++i];
else if (database == null) database = args[i]; // <dbname>
else if (table == null) table = args[i]; // <table>
else throw new IllegalArgumentException("Unknown argument: "
+args[i]);
}
// Make sure that at least a server or a database were specified.
// If not, we have no idea what to connect to, and cannot continue.
if ((server.length() == 0) && (database.length() == 0))
throw new IllegalArgumentException("No database specified.");
// Load the db driver, if any was specified.
if (driver != null) Class.forName(driver);
// Now attempt to open a connection to the specified database on
// the specified server, using the specified name and password
c = DriverManager.getConnection(server+database, user, password);
// Get the DatabaseMetaData object for the connection. This is the
// object that will return us all the data we're interested in here
DatabaseMetaData md = c.getMetaData();
// Display information about the server, the driver, etc.
System.out.println("DBMS: " + md.getDatabaseProductName() +
" " + md.getDatabaseProductVersion());
System.out.println("JDBC Driver: " + md.getDriverName() +
" " + md.getDriverVersion());
System.out.println("Database: " + md.getURL());
System.out.println("User: " + md.getUserName());
// Now, if the user did not specify a table, then display a list of
// all tables defined in the named database. Note that tables are
Search WWH ::




Custom Search