001: package examples;
002:
003: import jimm.util.Getopts;
004: import java.sql.*;
005:
006: /**
007: * This program lets you test your driver class name, connection info
008: * string, and all the other stuff that goes into the DataVision database
009: * connection dialog box.
010: * <p>
011: * To compile and run using Ant, specify the all of the command line args
012: * using the single property "cmdline". Here is an example:
013: * <pre>
014: * % ant -Dcmdline="-d driver -c conninfo" conntest
015: * </pre>
016: * <p>
017: * To compile and run using make, use "make conntest". To run, type the
018: * following on a single line (broken up for readability):
019: * <pre>
020: * java -classpath classes:path-to-driver.jar examples.ConnectionTest
021: * [-v] -d driver -c conninfo [-s schema] -u username [-p password]
022: * </pre>
023: * <p>
024: * Both the <b>-s</b> and <b>-p</b> options are...um...optional.
025: * <p>
026: * As an example, here is how I run the test:
027: * <pre>
028: * java -classpath classes:/usr/lib/pgsql/pgjdbc2.jar
029: * examples.ConnectionTest -d org.postgresql.Driver
030: * -c jdbc:postgresql://localhost/dv_example -s dv_example -u jimm -v
031: * </pre>
032: * <p>
033: * The <b>-v</b> option turns on verbose mode; messages describing the
034: * progress of establishing the connection are printed as is a list of
035: * all the table names in the schema.
036: * <p>
037: * The <b>-h</b> option prints a help message.
038: *
039: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
040: */
041: public class ConnectionTest {
042:
043: public static void usage() {
044: System.err
045: .println("usage: java [-classpath path] examples.ConnectionTest\n"
046: + " -d driver -c conn [-s schema] -u username [-p password]\n"
047: + " -d driver Driver class name\n"
048: + " -c conn Connection info string\n"
049: + " -s schema Database schema name (database name)\n"
050: + " -u username Database user name\n"
051: + " -p password Database password\n"
052: + "\n"
053: + " -v Verbose; print all table names found\n"
054: + " -h This help");
055: System.exit(1);
056: }
057:
058: public static void main(String[] args) {
059: Getopts g = new Getopts("hvd:c:s:u:p:", args);
060: if (g.error() || g.hasOption('h') || !g.hasOption('d')
061: || !g.hasOption('c') || !g.hasOption('u'))
062: usage();
063:
064: boolean verbose = g.hasOption('v');
065: try {
066: // Load the database JDBC driver
067: if (verbose)
068: System.out.println("loading driver");
069: Driver d = (Driver) Class.forName(g.option('d'))
070: .newInstance();
071: if (verbose)
072: System.out.println("registering driver");
073: DriverManager.registerDriver(d);
074:
075: // Connect to the database
076: if (verbose)
077: System.out.println("creating database connection");
078: Connection conn = DriverManager.getConnection(
079: g.option('c'), g.option('u'), g.option('p'));
080:
081: // If verbose, read table names and print them
082: if (verbose) {
083: DatabaseMetaData dbmd = conn.getMetaData();
084:
085: System.out.println("stores lower case identifiers = "
086: + dbmd.storesLowerCaseIdentifiers());
087: System.out.println("stores upper case identifiers = "
088: + dbmd.storesUpperCaseIdentifiers());
089:
090: System.out.println("tables:");
091: ResultSet rset = dbmd.getTables(null, g.option('s'),
092: "%", null);
093: while (rset.next())
094: System.out.println(" "
095: + rset.getString("TABLE_NAME"));
096: rset.close();
097: }
098:
099: if (verbose)
100: System.out.println("closing the connection");
101: conn.close();
102:
103: System.out.println("done");
104: } catch (SQLException sqle) {
105: SQLException ex = (SQLException) sqle;
106: ex = ex.getNextException();
107: while (ex != null) {
108: System.err.println(ex.toString());
109: ex = ex.getNextException();
110: }
111: sqle.printStackTrace();
112: System.exit(1);
113: } catch (Exception e) {
114: System.err.println(e.toString());
115: e.printStackTrace();
116: System.exit(1);
117: }
118: }
119:
120: }
|