1 package examples; 2 import jimm.util.Getopts; 3 import java.sql.*; 4 5 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 [] 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 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 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) { 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 e) { 109 System.err.println(e.toString()); 110 e.printStackTrace(); 111 System.exit(1); 112 } 113 } 114 115 } 116
| Popular Tags
|