|
| 1 | +packageexample; |
| 2 | + |
| 3 | +importjava.io.*; |
| 4 | +importjava.sql.*; |
| 5 | +importjava.text.*; |
| 6 | + |
| 7 | +/** |
| 8 | + * This example application is not really an example. It actually performs |
| 9 | + * some tests on various methods in the DatabaseMetaData and ResultSetMetaData |
| 10 | + * classes. |
| 11 | + * |
| 12 | + * To use it, simply have a database created. It will create some work tables |
| 13 | + * and run tests on them. |
| 14 | + */ |
| 15 | + |
| 16 | +publicclassmetadata |
| 17 | +{ |
| 18 | +Connectiondb;// The connection to the database |
| 19 | +Statementst;// Our statement to run queries with |
| 20 | +DatabaseMetaDatadbmd;// This defines the structure of the database |
| 21 | + |
| 22 | +/** |
| 23 | + * These are the available tests on DatabaseMetaData |
| 24 | + */ |
| 25 | +publicvoiddoDatabaseMetaData()throwsSQLException { |
| 26 | +if(doTest("getProcedures() - should show all available procedures")) |
| 27 | +displayResult(dbmd.getProcedures(null,null,null)); |
| 28 | + |
| 29 | +if(doTest("getProcedures() with pattern - should show all circle procedures")) |
| 30 | +displayResult(dbmd.getProcedures(null,null,"circle%")); |
| 31 | + |
| 32 | +if(doTest("getProcedureColumns() on circle procedures")) |
| 33 | +displayResult(dbmd.getProcedureColumns(null,null,"circle%",null)); |
| 34 | + |
| 35 | +if(doTest("getTables()")) |
| 36 | +displayResult(dbmd.getTables(null,null,null,null)); |
| 37 | + |
| 38 | +if(doTest("getColumns() - should show all tables, can take a while to run")) |
| 39 | +displayResult(dbmd.getColumns(null,null,null,null)); |
| 40 | + |
| 41 | +if(doTest("getColumns() - should show the test_b table")) |
| 42 | +displayResult(dbmd.getColumns(null,null,"test_b",null)); |
| 43 | + |
| 44 | +if(doTest("getColumnPrivileges() - should show all tables")) |
| 45 | +displayResult(dbmd.getColumnPrivileges(null,null,null,null)); |
| 46 | + |
| 47 | +if(doTest("getPrimaryKeys()")) |
| 48 | +displayResult(dbmd.getPrimaryKeys(null,null,null)); |
| 49 | + |
| 50 | +if(doTest("getTypeInfo()")) |
| 51 | +displayResult(dbmd.getTypeInfo()); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | +/** |
| 56 | + * These are the available tests on ResultSetMetaData |
| 57 | + */ |
| 58 | +publicvoiddoResultSetMetaData()throwsSQLException { |
| 59 | + |
| 60 | +Stringsql ="select imagename,descr,source,cost from test_a,test_b,test_c where test_a.id=test_b.imageid and test_a.id=test_c.imageid"; |
| 61 | + |
| 62 | +System.out.println("Executing query for tests"); |
| 63 | +ResultSetrs =st.executeQuery(sql); |
| 64 | +ResultSetMetaDatarsmd =rs.getMetaData(); |
| 65 | + |
| 66 | +if(doTest("isCurrency()")) |
| 67 | +System.out.println("isCurrency on col 1 = "+rsmd.isCurrency(1)+" should be false\nisCurrency on col 4 = "+rsmd.isCurrency(4)+" should be true"); |
| 68 | + |
| 69 | +// Finally close the query. Now give the user a chance to display the |
| 70 | +// ResultSet. |
| 71 | +// |
| 72 | +// NB: displayResult() actually closes the ResultSet. |
| 73 | +if(doTest("Display query result")) { |
| 74 | +System.out.println("Query: "+sql); |
| 75 | +displayResult(rs); |
| 76 | + }else |
| 77 | +rs.close(); |
| 78 | + } |
| 79 | + |
| 80 | +/** |
| 81 | + * This creates some test data |
| 82 | + */ |
| 83 | +publicvoidinit()throwsSQLException { |
| 84 | +System.out.println("Creating some tables"); |
| 85 | +cleanup(); |
| 86 | +st.executeUpdate("create table test_a (imagename name,image oid,id int4)"); |
| 87 | +st.executeUpdate("create table test_b (descr text,imageid int4,id int4)"); |
| 88 | +st.executeUpdate("create table test_c (source text,cost money,imageid int4)"); |
| 89 | + |
| 90 | +System.out.println("Adding some data"); |
| 91 | +st.executeUpdate("insert into test_a values ('test1',0,1)"); |
| 92 | +st.executeUpdate("insert into test_b values ('A test description',1,2)"); |
| 93 | +st.executeUpdate("insert into test_c values ('nowhere particular','$10.99',1)"); |
| 94 | + } |
| 95 | + |
| 96 | +/** |
| 97 | + * This removes the test data |
| 98 | + */ |
| 99 | +publicvoidcleanup()throwsSQLException { |
| 100 | +try { |
| 101 | +st.executeUpdate("drop table test_a"); |
| 102 | +st.executeUpdate("drop table test_b"); |
| 103 | +st.executeUpdate("drop table test_c"); |
| 104 | + }catch(Exceptionex) { |
| 105 | +// We ignore any errors here |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +publicmetadata(Stringargs[])throwsClassNotFoundException,FileNotFoundException,IOException,SQLException |
| 110 | + { |
| 111 | +Stringurl =args[0]; |
| 112 | +Stringusr =args[1]; |
| 113 | +Stringpwd =args[2]; |
| 114 | + |
| 115 | +// Load the driver |
| 116 | +Class.forName("postgresql.Driver"); |
| 117 | + |
| 118 | +// Connect to database |
| 119 | +System.out.println("Connecting to Database URL = " +url); |
| 120 | +db =DriverManager.getConnection(url,usr,pwd); |
| 121 | + |
| 122 | +dbmd =db.getMetaData(); |
| 123 | +st =db.createStatement(); |
| 124 | + |
| 125 | +// This prints the backend's version |
| 126 | +System.out.println("Connected to "+dbmd.getDatabaseProductName()+" "+dbmd.getDatabaseProductVersion()); |
| 127 | + |
| 128 | +init(); |
| 129 | + |
| 130 | +System.out.println(); |
| 131 | + |
| 132 | +// Now the tests |
| 133 | +if(doTest("Test DatabaseMetaData")) |
| 134 | +doDatabaseMetaData(); |
| 135 | + |
| 136 | +if(doTest("Test ResultSetMetaData")) |
| 137 | +doResultSetMetaData(); |
| 138 | + |
| 139 | +System.out.println("\nNow closing the connection"); |
| 140 | +st.close(); |
| 141 | +db.close(); |
| 142 | + |
| 143 | +cleanup(); |
| 144 | + } |
| 145 | + |
| 146 | +/** |
| 147 | + * This asks if the user requires to run a test. |
| 148 | + */ |
| 149 | +publicbooleandoTest(Strings) { |
| 150 | +System.out.println(); |
| 151 | +System.out.print(s); |
| 152 | +System.out.print(" Perform test? Y or N:"); |
| 153 | +System.out.flush(); |
| 154 | +charc =' '; |
| 155 | +try { |
| 156 | +while(!(c=='n' ||c=='y' ||c=='N' ||c=='Y')) { |
| 157 | +c=(char)System.in.read(); |
| 158 | + } |
| 159 | + }catch(IOExceptionioe) { |
| 160 | +returnfalse; |
| 161 | + } |
| 162 | + |
| 163 | +returnc=='y' ||c=='Y'; |
| 164 | + } |
| 165 | + |
| 166 | +/** |
| 167 | + * This displays a result set. |
| 168 | + * Note: it closes the result once complete. |
| 169 | + */ |
| 170 | +publicvoiddisplayResult(ResultSetrs)throwsSQLException |
| 171 | + { |
| 172 | +ResultSetMetaDatarsmd =rs.getMetaData(); |
| 173 | +intcount=0; |
| 174 | + |
| 175 | +// Print the result column names |
| 176 | +intcols =rsmd.getColumnCount(); |
| 177 | +for(inti=1;i<=cols;i++) |
| 178 | +System.out.print(rsmd.getColumnLabel(i)+(i<cols?"\t":"\n")); |
| 179 | + |
| 180 | +// now the results |
| 181 | +while(rs.next()) { |
| 182 | +count++; |
| 183 | +for(inti=1;i<=cols;i++) { |
| 184 | +Objecto =rs.getObject(i); |
| 185 | +if(rs.wasNull()) |
| 186 | +System.out.print("{null}"+(i<cols?"\t":"\n")); |
| 187 | +else |
| 188 | +System.out.print(o.toString()+(i<cols?"\t":"\n")); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | +System.out.println("Result returned "+count+" rows."); |
| 193 | + |
| 194 | +// finally close the result set |
| 195 | +rs.close(); |
| 196 | + } |
| 197 | + |
| 198 | +/** |
| 199 | + * This process / commands (for now just /d) |
| 200 | + */ |
| 201 | +publicvoidprocessSlashCommand(Stringline)throwsSQLException |
| 202 | + { |
| 203 | +if(line.startsWith("\\d")) { |
| 204 | + |
| 205 | +if(line.startsWith("\\d ")) { |
| 206 | +// Display details about a table |
| 207 | +Stringtable=line.substring(3); |
| 208 | +displayResult(dbmd.getColumns(null,null,table,"%")); |
| 209 | + }else { |
| 210 | +Stringtypes[] =null; |
| 211 | +if(line.equals("\\d")) |
| 212 | +types=allUserTables; |
| 213 | +elseif(line.equals("\\di")) |
| 214 | +types=usrIndices; |
| 215 | +elseif(line.equals("\\dt")) |
| 216 | +types=usrTables; |
| 217 | +elseif(line.equals("\\ds")) |
| 218 | +types=usrSequences; |
| 219 | +elseif(line.equals("\\dS")) |
| 220 | +types=sysTables; |
| 221 | +else |
| 222 | +thrownewSQLException("Unsupported\\d command: "+line); |
| 223 | + |
| 224 | +// Display details about all system tables |
| 225 | +// |
| 226 | +// Note: the first two arguments are ignored. To keep to the spec, |
| 227 | +// you must put null here |
| 228 | +// |
| 229 | +displayResult(dbmd.getTables(null,null,"%",types)); |
| 230 | + } |
| 231 | + }else |
| 232 | +thrownewSQLException("Unsupported\\ command: "+line); |
| 233 | + } |
| 234 | + |
| 235 | +privatestaticfinalStringallUserTables[] = {"TABLE","INDEX","SEQUENCE"}; |
| 236 | +privatestaticfinalStringusrIndices[] = {"INDEX"}; |
| 237 | +privatestaticfinalStringusrTables[] = {"TABLE"}; |
| 238 | +privatestaticfinalStringusrSequences[] = {"SEQUENCE"}; |
| 239 | +privatestaticfinalStringsysTables[] = {"SYSTEM TABLE","SYSTEM INDEX"}; |
| 240 | + |
| 241 | +/** |
| 242 | + * Display some instructions on how to run the example |
| 243 | + */ |
| 244 | +publicstaticvoidinstructions() |
| 245 | + { |
| 246 | +System.out.println("\nThis is not really an example, but is used to test the various methods in\nthe DatabaseMetaData and ResultSetMetaData classes.\n"); |
| 247 | +System.out.println("Useage:\n java example.metadata jdbc:postgresql:database user password [debug]\n\nThe debug field can be anything. It's presence will enable DriverManager's\ndebug trace. Unless you want to see screens of debug items, don't put anything in\nhere."); |
| 248 | +System.exit(1); |
| 249 | + } |
| 250 | + |
| 251 | +/** |
| 252 | + * This little lot starts the test |
| 253 | + */ |
| 254 | +publicstaticvoidmain(Stringargs[]) |
| 255 | + { |
| 256 | +System.out.println("PostgreSQL metdata tester v6.4 rev 1\n"); |
| 257 | + |
| 258 | +if(args.length<3) |
| 259 | +instructions(); |
| 260 | + |
| 261 | +// This line outputs debug information to stderr. To enable this, simply |
| 262 | +// add an extra parameter to the command line |
| 263 | +if(args.length>3) |
| 264 | +DriverManager.setLogStream(System.err); |
| 265 | + |
| 266 | +// Now run the tests |
| 267 | +try { |
| 268 | +metadatatest =newmetadata(args); |
| 269 | + }catch(Exceptionex) { |
| 270 | +System.err.println("Exception caught.\n"+ex); |
| 271 | +ex.printStackTrace(); |
| 272 | + } |
| 273 | + } |
| 274 | +} |