/Users/lyon/j4p/src/addBk/address/SimpleSelect.java
|
1 package addBk.address;
2
3 import java.awt.*;
4 import java.sql.*;
5 import java.util.Vector;
6
7 public class SimpleSelect {
8 private String url =
9 "jdbc:odbc:AddressBook";
10 private String query =
11 "SELECT * FROM ";
12 private String driverClassName =
13 "sun.jdbc.odbc.JdbcOdbcDriver";
14
15 private Connection con = null;
16 private String catalogName = null;
17 private DatabaseMetaData dma = null;
18 private Statement stmt = null;
19 private ResultSet rs = null;
20 private ResultSetMetaData rsmd = null;
21 private String keywords = null;
22 private boolean isReadOnly = false;
23 private boolean usesLocalFiles = false;
24 private String driverName = null;
25
26 private String selectedTable = null;
27
28 private ResultSet tables_rs = null;
29 private ResultSetMetaData
30 tables_rsmd = null;
31 private String tableFields[] = null;
32
33 private Vector tableVector =
34 new Vector();
35 private String tableNames[] = null;
36
37 private String nonMSysTables[] = null;
38
39 private Vector v = null;
40
41
42 public void connect() {
43 try {
44 safeConnect();
45 } catch (SQLException e) {
46 print(e);
47 } catch (ClassNotFoundException e) {
48 print(e);
49 }
50 }
51
52 public void safeConnect() throws
53 SQLException, ClassNotFoundException {
54 safeLoadDriver();
55 //DriverManager.setLogStream(System.out);
56 con =
57 DriverManager.getConnection(
58 url, "", "");
59 dma = con.getMetaData();
60 catalogName =
61 con.getCatalog();
62 isReadOnly = con.isReadOnly();
63 usesLocalFiles =
64 dma.usesLocalFiles();
65 driverName =
66 dma.getDriverName();
67
68 initTableNames();
69 createStatement();
70 }
71
72 public String getSelectedTable() {
73 return selectedTable;
74 }
75
76 public void createStatement() {
77 try {
78 stmt = con.createStatement();
79 } catch (SQLException e) {
80 print(e);
81 }
82 }
83
84 public void printKeywords() {
85 println("Non SL92 keywords:");
86 println(getKeywords());
87 }
88
89 public String[] getNonMSysTableNames() {
90 return nonMSysTables;
91 }
92
93 public void initNonMSysTables() {
94 int n
95 = tableNames.length -
96 getNumberOfMSysTables();
97 nonMSysTables = new String[n];
98 for (int i = 0; i < n; i++) {
99 nonMSysTables[i]
100 = tableNames[i + getNumberOfMSysTables()];
101 }
102
103 }
104
105 private int getNumberOfMSysTables() {
106 int k = 0;
107 for (int i = 0; i < tableNames.length; i++)
108 if (tableNames[i].startsWith("MSys"))
109 k++;
110 return k;
111 }
112
113 private void initTableNames() {
114 try {
115 tables_rs =
116 dma.getTables(
117 null,
118 null, null, null);
119 tables_rsmd =
120 tables_rs.getMetaData();
121 tableFields =
122 getColumnNames(tables_rsmd);
123 while (tables_rs.next())
124 tableVector.addElement(
125 tables_rs.getString("TABLE_NAME"));
126 } catch (SQLException e) {
127 print(e);
128 }
129 int n = tableVector.size();
130 tableNames =
131 new String[n];
132 for (int i = 0; i < n; i++)
133 tableNames[i]
134 = (String)
135 tableVector.elementAt(i);
136 initNonMSysTables();
137
138 }
139
140 public String[] getTableNames() {
141 return tableNames;
142 }
143
144 public void printProductName() {
145 print("The database product name is:");
146 println(getProductName());
147 }
148
149 public String getProductName() {
150 try {
151 return
152 dma.getDatabaseProductName();
153 } catch (SQLException e) {
154 print(e);
155 }
156 return null;
157 }
158
159 public void printTableTypes() {
160 println("The table types are:");
161 ResultSet rs
162 = getTableTypes();
163 try {
164 while (rs.next()) {
165 String s = rs.getString("TABLE_TYPE");
166 println(" " + s);
167 }
168 } catch (SQLException e) {
169 print(e);
170 }
171
172 }
173
174 public ResultSet getTableTypes() {
175 try {
176 return
177 dma.getTableTypes();
178 } catch (SQLException e) {
179 print(e);
180 }
181 return null;
182 }
183
184 /**
185 keywords that are not also SQL92 keywords.
186 */
187 public String getKeywords() {
188 try {
189 return
190 dma.getSQLKeywords();
191 } catch (SQLException e) {
192 print(e);
193 }
194 return null;
195 }
196
197 public void loadDriver() {
198 try {
199 safeLoadDriver();
200 } catch (ClassNotFoundException e) {
201 println(e);
202 }
203 }
204
205 public void safeLoadDriver() throws ClassNotFoundException {
206 Class.forName(
207 driverClassName);
208 }
209
210 public void printInfo() {
211 printProductName();
212 println("catalogName=" + catalogName);
213 printTableTypes();
214 printKeywords();
215 println("is ReadOnly=" + getReadOnly());
216 println("usesLocalFiles=" + getUsesLocalFiles());
217 println("driverName=" + driverName);
218 println("tableFields=");
219 print(tableFields);
220 println("tableNames=");
221 print(tableNames);
222 println("number of MSysTables="
223 + getNumberOfMSysTables());
224 println("Non MSysTables=");
225 print(getNonMSysTableNames());
226
227 }
228
229 public boolean getUsesLocalFiles() {
230 return usesLocalFiles;
231 }
232
233 public boolean getReadOnly() {
234 return isReadOnly;
235 }
236
237 public String getCatalogName() {
238 return catalogName;
239 }
240
241
242 public String getDriverName() {
243 return driverName;
244 }
245
246 public void print(Object o) {
247 System.out.print(o);
248 }
249
250 public void println(Object o) {
251 System.out.println(o);
252 }
253
254 public void println(String s[]) {
255 for (int i = 0; i < s.length; i++)
256 System.out.println(s[i]);
257 }
258
259 public void getQuery() {
260 getQuery("Authors");
261 }
262
263 public void getQuery(String _selectedTable) {
264 selectedTable = _selectedTable;
265 query =
266 "SELECT * FROM " + selectedTable;
267 try {
268 rs = stmt.executeQuery(query);
269 rsmd = rs.getMetaData();
270 } catch (SQLException e) {
271 print(e);
272 }
273 }
274
275 public void setQuery(String q) {
276 query = q;
277 rs = null;
278 }
279
280 public void printDebug() {
281 try {
282 checkForWarning(con.getWarnings());
283 println("\nConnected to " + dma.getURL());
284 println("Driver " +
285 dma.getDriverName());
286 println("Version " +
287 dma.getDriverVersion());
288 println("");
289 } catch (SQLException e) {
290 print(e);
291 }
292 }
293
294 public void display() {
295 try {
296 dispResultSet();
297 } catch (SQLException e) {
298 print(e);
299 }
300 }
301
302 private void print(SQLException ex) {
303 println("\n*** SQLException caught ***\n");
304 while (ex != null) {
305 println("SQLState: " +
306 ex.getSQLState());
307 println("Message: " + ex.getMessage());
308 println("Vendor: " +
309 ex.getErrorCode());
310 ex = ex.getNextException();
311 println("");
312 }
313 // Got some other type of exception. Dump it.
314 ex.printStackTrace();
315 }
316
317 public void print(String s[]) {
318 for (int i = 0; i < s.length; i++)
319 print(s[i] + ",");
320 println("");
321 }
322
323 public void close() {
324 try {
325 rs.close();
326 stmt.close();
327 con.close();
328 } catch (SQLException e) {
329 print(e);
330 }
331 }
332
333 public static void main(String args[]) {
334 SimpleSelect ss
335 = new SimpleSelect();
336 ss.connect();
337 ss.getQuery();
338 //ss.printColumnNames();
339 ss.printInfo();
340 //ss.printRows();
341 ss.close();
342 }
343 //-------------------------------------------------------------------
344 // checkForWarning
345 // Checks for and displays warnings. Returns true if a warning
346 // existed
347 //-------------------------------------------------------------------
348 private boolean checkForWarning(SQLWarning warn) {
349 boolean rc = false;
350 // If a SQLWarning object was given, display the
351 // warning messages. Note that there could be
352 // multiple warnings chained together
353 if (warn != null) {
354 println("\n *** Warning ***\n");
355 rc = true;
356 while (warn != null) {
357 println("SQLState: " +
358 warn.getSQLState());
359 println("Message: " +
360 warn.getMessage());
361 println("Vendor: " +
362 warn.getErrorCode());
363 println("");
364 warn = warn.getNextWarning();
365 }
366 }
367 return rc;
368 }
369
370 public String[] getColumnNames() {
371 return getColumnNames(rsmd);
372 }
373
374 public String[] getColumnNames(ResultSetMetaData r) {
375 String s []
376 = new String[getNumberOfColumns(r)];
377 try {
378 for (int i = 1; i <= s.length; i++)
379 s[i - 1] = r.getColumnLabel(i);
380 } catch (SQLException e) {
381 print(e);
382 }
383
384 return s;
385 }
386
387 public String[] getColumnNames(ResultSet r) {
388 try {
389 return
390 getColumnNames(
391 r.getMetaData());
392 } catch (SQLException e) {
393 print(e);
394 }
395 return null;
396 }
397
398 public Label[] getColumnLabels() {
399 String s[]
400 = getColumnNames();
401 Label l[] = new Label[s.length];
402 for (int i = 0; i < s.length; i++)
403 l[i] = new Label(s[i]);
404 return l;
405 }
406
407 public void printNumberOfColumns() {
408 System.out.println(
409 getNumberOfColumns());
410 }
411
412 public int getNumberOfColumns() {
413 return getNumberOfColumns(rsmd);
414 }
415
416 private int getNumberOfColumns(ResultSetMetaData r) {
417 int i = -1;
418 try {
419 i = r.getColumnCount();
420 } catch (SQLException e) {
421 print(e);
422 }
423 return i;
424 }
425
426 public void printColumnNames() {
427 String s[] = getColumnNames();
428 for (int i = 0; i < s.length; i++)
429 print(s[i] + ",");
430 println("");
431 }
432
433 public String[] getRowAsString() {
434 int N = getNumberOfColumns();
435 String s[] = new String[N];
436 try {
437 for (int i = 0; i < N; i++)
438 s[i] = rs.getString(i + 1);
439 } catch (SQLException e) {
440 print(e);
441 }
442 return s;
443 }
444
445 public void printVector() {
446 for (int i = 0; i < v.size(); i++) {
447 String s[]
448 = getRowAt(i);
449 print(s);
450 }
451 }
452
453 public int getNumberOfRows() {
454 return v.size();
455 }
456
457 public String[] getRowAt(int i) {
458 return (String[]) v.elementAt(i);
459 }
460
461 public void printRow() {
462 String s[]
463 = getRowAsString();
464 for (int i = 0; i < s.length; i++) {
465 print(s[i] + ", ");
466 }
467 println("");
468 }
469
470 /**
471 Download entire database!
472 Danger Will Robinson,
473 Take cover Dr. Smith!!
474 */
475 public Vector getRows() {
476 if (v != null) return v;
477 v = new Vector();
478 while (nextRow())
479 v.addElement(getRowAsString());
480 return v;
481 }
482
483 public void printRows() {
484 while (nextRow())
485 printRow();
486 }
487
488 public boolean nextRow() {
489 try {
490 return rs.next();
491 } catch (SQLException e) {
492 print(e);
493 return false;
494 }
495 }
496
497
498 //-------------------------------------------------------------------
499 // dispResultSet
500 // Displays all columns and rows in the given result set
501 //-------------------------------------------------------------------
502 private void dispResultSet() throws SQLException {
503 int i;
504 printColumnNames();
505 // Display data, fetching until end of the result set
506 boolean more = rs.next();
507 int numCols = getNumberOfColumns();
508 while (more) {
509 // Loop through each column, getting the
510 // column data and displaying
511 for (i = 1; i <= numCols; i++) {
512 if (i > 1) print(",");
513 print(rs.getString(i));
514 }
515 println("");
516 // Fetch the next result set row
517 more = rs.next();
518 }
519 }
520
521 }