KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > ConnectionTest


1 package examples;
2 import jimm.util.Getopts;
3 import java.sql.*;
4
5 /**
6  * This program lets you test your driver class name, connection info
7  * string, and all the other stuff that goes into the DataVision database
8  * connection dialog box.
9  * <p>
10  * To compile and run using Ant, specify the all of the command line args
11  * using the single property "cmdline". Here is an example:
12  * <pre>
13  * % ant -Dcmdline="-d driver -c conninfo" conntest
14  * </pre>
15  * <p>
16  * To compile and run using make, use "make conntest". To run, type the
17  * following on a single line (broken up for readability):
18  * <pre>
19  * java -classpath classes:path-to-driver.jar examples.ConnectionTest
20  * [-v] -d driver -c conninfo [-s schema] -u username [-p password]
21  * </pre>
22  * <p>
23  * Both the <b>-s</b> and <b>-p</b> options are...um...optional.
24  * <p>
25  * As an example, here is how I run the test:
26  * <pre>
27  * java -classpath classes:/usr/lib/pgsql/pgjdbc2.jar
28  * examples.ConnectionTest -d org.postgresql.Driver
29  * -c jdbc:postgresql://localhost/dv_example -s dv_example -u jimm -v
30  * </pre>
31  * <p>
32  * The <b>-v</b> option turns on verbose mode; messages describing the
33  * progress of establishing the connection are printed as is a list of
34  * all the table names in the schema.
35  * <p>
36  * The <b>-h</b> option prints a help message.
37  *
38  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
39  */

40 public class ConnectionTest {
41
42 public static void usage() {
43     System.err.println(
44 "usage: java [-classpath path] examples.ConnectionTest\n" +
45 " -d driver -c conn [-s schema] -u username [-p password]\n" +
46 " -d driver Driver class name\n" +
47 " -c conn Connection info string\n" +
48 " -s schema Database schema name (database name)\n" +
49 " -u username Database user name\n" +
50 " -p password Database password\n" +
51 "\n" +
52 " -v Verbose; print all table names found\n" +
53 " -h This help");
54     System.exit(1);
55 }
56
57 public static void main(String JavaDoc[] args) {
58     Getopts g = new Getopts("hvd:c:s:u:p:", args);
59     if (g.error() || g.hasOption('h') || !g.hasOption('d') || !g.hasOption('c')
60     || !g.hasOption('u'))
61     usage();
62
63     boolean verbose = g.hasOption('v');
64     try {
65     // Load the database JDBC driver
66
if (verbose) System.out.println("loading driver");
67     Driver d = (Driver)Class.forName(g.option('d')).newInstance();
68     if (verbose) System.out.println("registering driver");
69     DriverManager.registerDriver(d);
70
71     // Connect to the database
72
if (verbose) System.out.println("creating database connection");
73     Connection conn = DriverManager.getConnection(g.option('c'),
74                               g.option('u'),
75                               g.option('p'));
76
77     // If verbose, read table names and print them
78
if (verbose) {
79         DatabaseMetaData dbmd = conn.getMetaData();
80
81         System.out.println("stores lower case identifiers = "
82                    + dbmd.storesLowerCaseIdentifiers());
83         System.out.println("stores upper case identifiers = "
84                    + dbmd.storesUpperCaseIdentifiers());
85
86         System.out.println("tables:");
87         ResultSet rset = dbmd.getTables(null, g.option('s'), "%", null);
88         while (rset.next())
89         System.out.println(" " + rset.getString("TABLE_NAME"));
90         rset.close();
91     }
92
93     if (verbose) System.out.println("closing the connection");
94     conn.close();
95
96     System.out.println("done");
97     }
98     catch (SQLException sqle) {
99     SQLException ex = (SQLException)sqle;
100     ex = ex.getNextException();
101     while (ex != null) {
102         System.err.println(ex.toString());
103         ex = ex.getNextException();
104     }
105     sqle.printStackTrace();
106     System.exit(1);
107     }
108     catch (Exception JavaDoc e) {
109     System.err.println(e.toString());
110     e.printStackTrace();
111     System.exit(1);
112     }
113 }
114
115 }
116
Popular Tags