1 30 31 32 package org.hsqldb.util; 33 34 import java.applet.Applet ; 35 import java.io.File ; 36 import java.io.IOException ; 37 import java.sql.Connection ; 38 import java.sql.DatabaseMetaData ; 39 import java.sql.ResultSet ; 40 import java.sql.ResultSetMetaData ; 41 import java.sql.SQLException ; 42 import java.sql.Statement ; 43 import java.util.Vector ; 44 import java.awt.BorderLayout ; 45 import java.awt.Button ; 46 import java.awt.Color ; 47 import java.awt.Dimension ; 48 import java.awt.FileDialog ; 49 import java.awt.Font ; 50 import java.awt.Frame ; 51 import java.awt.Image ; 52 import java.awt.Menu ; 53 import java.awt.MenuBar ; 54 import java.awt.MenuItem ; 55 import java.awt.MenuShortcut ; 56 import java.awt.Panel ; 57 import java.awt.TextArea ; 58 import java.awt.Toolkit ; 59 import java.awt.event.ActionEvent ; 60 import java.awt.event.ActionListener ; 61 import java.awt.event.KeyEvent ; 62 import java.awt.event.KeyListener ; 63 import java.awt.event.WindowEvent ; 64 import java.awt.event.WindowListener ; 65 import java.awt.image.MemoryImageSource ; 66 67 import org.hsqldb.lib.java.JavaSystem; 68 69 74 98 public class DatabaseManager extends Applet 99 implements ActionListener , WindowListener , KeyListener { 100 101 private static final String DEFAULT_RCFILE = 102 System.getProperty("user.home") + "/dbmanager.rc"; 103 static final String NL = System.getProperty("line.separator"); 104 static final int iMaxRecent = 24; 105 Connection cConn; 106 DatabaseMetaData dMeta; 107 Statement sStatement; 108 Menu mRecent; 109 String [] sRecent; 110 int iRecent; 111 TextArea txtCommand; 112 Button butExecute; 113 Button butClear; 114 Tree tTree; 115 Panel pResult; 116 long lTime; 117 int iResult; Grid gResult; 119 TextArea txtResult; 120 boolean bHelp; 121 Frame fMain; 122 Image imgEmpty; 123 static boolean bMustExit; 124 String ifHuge = ""; 125 126 static String defDriver = "org.hsqldb.jdbcDriver"; 128 static String defURL = "jdbc:hsqldb:."; 129 static String defUser = "sa"; 130 static String defPassword = ""; 131 static String defScript; 132 static String defDirectory; 133 134 140 public void connect(Connection c) { 141 142 if (c == null) { 143 return; 144 } 145 146 if (cConn != null) { 147 try { 148 cConn.close(); 149 } catch (SQLException e) {} 150 } 151 152 cConn = c; 153 154 try { 155 dMeta = cConn.getMetaData(); 156 sStatement = cConn.createStatement(); 157 158 refreshTree(); 159 } catch (SQLException e) { 160 e.printStackTrace(); 161 } 162 } 163 164 168 public void init() { 169 170 DatabaseManager m = new DatabaseManager(); 171 172 m.main(); 173 174 try { 175 m.connect(ConnectionDialog.createConnection(defDriver, defURL, 176 defUser, defPassword)); 177 m.insertTestData(); 178 m.refreshTree(); 179 } catch (Exception e) { 180 e.printStackTrace(); 181 } 182 } 183 184 190 public static void main(String [] arg) { 191 192 System.getProperties().put("sun.java2d.noddraw", "true"); 193 194 String lowerArg; 196 String urlid = null; 197 String rcFile = null; 198 boolean autoConnect = false; 199 boolean urlidConnect = false; 200 201 bMustExit = true; 202 203 for (int i = 0; i < arg.length; i++) { 204 lowerArg = arg[i].toLowerCase(); 205 206 if (lowerArg.length() > 1 && lowerArg.charAt(1) == '-') { 207 lowerArg = lowerArg.substring(1); 208 } 209 210 i++; 211 212 if (i == arg.length) { 213 showUsage(); 214 215 return; 216 } 217 218 if (lowerArg.equals("-driver")) { 219 defDriver = arg[i]; 220 autoConnect = true; 221 } else if (lowerArg.equals("-url")) { 222 defURL = arg[i]; 223 autoConnect = true; 224 } else if (lowerArg.equals("-user")) { 225 defUser = arg[i]; 226 autoConnect = true; 227 } else if (lowerArg.equals("-password")) { 228 defPassword = arg[i]; 229 autoConnect = true; 230 } else if (lowerArg.equals("-urlid")) { 231 urlid = arg[i]; 232 urlidConnect = true; 233 } else if (lowerArg.equals("-rcfile")) { 234 rcFile = arg[i]; 235 urlidConnect = true; 236 } else if (lowerArg.equals("-dir")) { 237 defDirectory = arg[i]; 238 } else if (lowerArg.equals("-script")) { 239 defScript = arg[i]; 240 } else if (lowerArg.equals("-noexit")) { 241 bMustExit = false; 242 243 i--; 244 } else { 245 showUsage(); 246 247 return; 248 } 249 } 250 251 DatabaseManager m = new DatabaseManager(); 252 253 m.main(); 254 255 Connection c = null; 256 257 try { 258 if (autoConnect && urlidConnect) { 259 throw new IllegalArgumentException ( 260 "You may not specify both (urlid) AND (url/user/password)."); 261 } 262 263 if (autoConnect) { 264 c = ConnectionDialog.createConnection(defDriver, defURL, 265 defUser, defPassword); 266 } else if (urlidConnect) { 267 if (urlid == null) { 268 throw new IllegalArgumentException ( 269 "You must specify an 'urlid' to use an RC file"); 270 } 271 272 autoConnect = true; 273 c = (new RCData(new File ((rcFile == null) ? DEFAULT_RCFILE 274 : rcFile), urlid).getConnection( 275 null, System.getProperty( 276 "sqlfile.charset"), System.getProperty( 277 "javax.net.ssl.trustStore"))); 278 } else { 279 c = ConnectionDialog.createConnection(m.fMain, "Connect"); 280 } 281 } catch (Exception e) { 282 e.printStackTrace(); 283 } 284 285 if (c == null) { 286 return; 287 } 288 289 m.connect(c); 290 } 291 292 private static void showUsage() { 293 294 System.out.println( 295 "Usage: java DatabaseManager [--options]\n" 296 + "where options include:\n" 297 + " --driver <classname> jdbc driver class\n" 298 + " --url <name> jdbc url\n" 299 + " --user <name> username used for connection\n" 300 + " --password <password> password for this user\n" 301 + " --urlid <urlid> use url/user/password/driver in rc file\n" 302 + " --rcfile <file> (defaults to 'dbmanager.rc' in home dir)\n" 303 + " --dir <path> default directory\n" 304 + " --script <file> reads from script file\n" 305 + " --noexit do not call system.exit()\n" 306 + "(Single-hypen switches like '-driver' are also supported)"); 307 } 308 309 313 void insertTestData() { 314 315 try { 316 DatabaseManagerCommon.createTestTables(sStatement); 317 refreshTree(); 318 txtCommand.setText( 319 DatabaseManagerCommon.createTestData(sStatement)); 320 refreshTree(); 321 322 for (int i = 0; i < DatabaseManagerCommon.testDataSql.length; 323 i++) { 324 addToRecent(DatabaseManagerCommon.testDataSql[i]); 325 } 326 327 execute(); 328 } catch (SQLException e) { 329 e.printStackTrace(); 330 } 331 } 332 333 337 public void main() { 338 339 fMain = new Frame ("HSQL Database Manager"); 340 imgEmpty = createImage(new MemoryImageSource (2, 2, new int[4 * 4], 2, 341 2)); 342 343 fMain.setIconImage(imgEmpty); 344 fMain.addWindowListener(this); 345 346 MenuBar bar = new MenuBar (); 347 348 String [] fitems = { 350 "-Connect...", "--", "-Open Script...", "-Save Script...", 351 "-Save Result...", "-Save Result csv...", "--", "-Exit" 352 }; 353 354 addMenu(bar, "File", fitems); 355 356 String [] vitems = { 357 "RRefresh Tree", "--", "GResults in Grid", "TResults in Text", 358 "--", "1Shrink Tree", "2Enlarge Tree", "3Shrink Command", 359 "4Enlarge Command" 360 }; 361 362 addMenu(bar, "View", vitems); 363 364 String [] sitems = { 365 "SSELECT", "IINSERT", "UUPDATE", "DDELETE", "--", "-CREATE TABLE", 366 "-DROP TABLE", "-CREATE INDEX", "-DROP INDEX", "--", 367 "-CHECKPOINT", "-SCRIPT", "-SET", "-SHUTDOWN", "--", 368 "-Test Script" 369 }; 370 371 addMenu(bar, "Command", sitems); 372 373 Menu recent = new Menu ("Recent"); 374 375 mRecent = new Menu ("Recent"); 376 377 bar.add(mRecent); 378 379 String [] soptions = { 380 "-AutoCommit on", "-AutoCommit off", "OCommit", "LRollback", "--", 381 "-Disable MaxRows", "-Set MaxRows to 100", "--", "-Logging on", 382 "-Logging off", "--", "-Insert test data" 383 }; 384 385 addMenu(bar, "Options", soptions); 386 387 String [] stools = { 388 "-Dump", "-Restore", "-Transfer" 389 }; 390 391 addMenu(bar, "Tools", stools); 392 fMain.setMenuBar(bar); 393 fMain.setSize(640, 480); 394 fMain.add("Center", this); 395 initGUI(); 396 397 sRecent = new String [iMaxRecent]; 398 399 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 400 Dimension size = fMain.getSize(); 401 402 if (d.width >= 640) { 404 fMain.setLocation((d.width - size.width) / 2, 405 (d.height - size.height) / 2); 406 } else { 407 fMain.setLocation(0, 0); 408 fMain.setSize(d); 409 } 410 411 fMain.show(); 412 413 if (defScript != null) { 415 if (defDirectory != null) { 416 defScript = defDirectory + File.separator + defScript; 417 } 418 419 txtCommand.setText(DatabaseManagerCommon.readFile(defScript)); 420 } 421 422 txtCommand.requestFocus(); 423 } 424 425 433 void addMenu(MenuBar b, String name, String [] items) { 434 435 Menu menu = new Menu (name); 436 437 addMenuItems(menu, items); 438 b.add(menu); 439 } 440 441 448 void addMenuItems(Menu f, String [] m) { 449 450 for (int i = 0; i < m.length; i++) { 451 MenuItem item = new MenuItem (m[i].substring(1)); 452 char c = m[i].charAt(0); 453 454 if (c != '-') { 455 item.setShortcut(new MenuShortcut (c)); 456 } 457 458 item.addActionListener(this); 459 f.add(item); 460 } 461 } 462 463 469 public void keyPressed(KeyEvent k) {} 470 471 477 public void keyReleased(KeyEvent k) {} 478 479 485 public void keyTyped(KeyEvent k) { 486 487 if (k.getKeyChar() == '\n' && k.isControlDown()) { 488 k.consume(); 489 execute(); 490 } 491 } 492 493 499 public void actionPerformed(ActionEvent ev) { 500 501 String s = ev.getActionCommand(); 502 503 if (s == null) { 504 if (ev.getSource() instanceof MenuItem ) { 505 MenuItem i; 506 507 s = ((MenuItem ) ev.getSource()).getLabel(); 508 } 509 } 510 511 if (s == null) {} 512 else if (s.equals("Execute")) { 513 execute(); 514 } else if (s.equals("Clear")) { 515 clear(); 516 } else if (s.equals("Exit")) { 517 windowClosing(null); 518 } else if (s.equals("Transfer")) { 519 Transfer.work(null); 520 } else if (s.equals("Dump")) { 521 Transfer.work(new String []{ "-d" }); 522 } else if (s.equals("Restore")) { 523 Transfer.work(new String []{ "-r" }); 524 } else if (s.equals("Logging on")) { 525 JavaSystem.setLogToSystem(true); 526 } else if (s.equals("Logging off")) { 527 JavaSystem.setLogToSystem(false); 528 } else if (s.equals("Refresh Tree")) { 529 refreshTree(); 530 } else if (s.startsWith("#")) { 531 int i = Integer.parseInt(s.substring(1)); 532 533 txtCommand.setText(sRecent[i]); 534 } else if (s.equals("Connect...")) { 535 connect(ConnectionDialog.createConnection(fMain, "Connect")); 536 refreshTree(); 537 } else if (s.equals("Results in Grid")) { 538 iResult = 0; 539 540 pResult.removeAll(); 541 pResult.add("Center", gResult); 542 pResult.doLayout(); 543 } else if (s.equals("Open Script...")) { 544 FileDialog f = new FileDialog (fMain, "Open Script", 545 FileDialog.LOAD); 546 547 if (defDirectory != null) { 549 f.setDirectory(defDirectory); 550 } 551 552 f.show(); 553 554 String file = f.getFile(); 555 556 if (file != null) { 557 StringBuffer buf = new StringBuffer (); 558 559 ifHuge = DatabaseManagerCommon.readFile(f.getDirectory() 560 + file); 561 562 if (4096 <= ifHuge.length()) { 563 buf.append( 564 "This huge file cannot be edited. Please execute\n"); 565 txtCommand.setText(buf.toString()); 566 } else { 567 txtCommand.setText(ifHuge); 568 } 569 } 570 } else if (s.equals("Save Script...")) { 571 FileDialog f = new FileDialog (fMain, "Save Script", 572 FileDialog.SAVE); 573 574 if (defDirectory != null) { 576 f.setDirectory(defDirectory); 577 } 578 579 f.show(); 580 581 String file = f.getFile(); 582 583 if (file != null) { 584 DatabaseManagerCommon.writeFile(f.getDirectory() + file, 585 txtCommand.getText()); 586 } 587 } else if (s.equals("Save Result csv...")) { 588 FileDialog f = new FileDialog (fMain, "Save Result CSV", 589 FileDialog.SAVE); 590 591 if (defDirectory != null) { 593 f.setDirectory(defDirectory); 594 } 595 596 f.show(); 597 598 String dir = f.getDirectory(); 599 String file = f.getFile(); 600 601 if (dir != null) { 602 file = dir + "/" + file; 603 } 604 605 if (file != null) { 606 showResultInText(); 607 saveAsCsv(file); 608 } 609 } else if (s.equals("Save Result...")) { 610 FileDialog f = new FileDialog (fMain, "Save Result", 611 FileDialog.SAVE); 612 613 if (defDirectory != null) { 615 f.setDirectory(defDirectory); 616 } 617 618 f.show(); 619 620 String file = f.getFile(); 621 622 if (file != null) { 623 showResultInText(); 624 DatabaseManagerCommon.writeFile(f.getDirectory() + file, 625 txtResult.getText()); 626 } 627 } else if (s.equals("Results in Text")) { 628 iResult = 1; 629 630 pResult.removeAll(); 631 pResult.add("Center", txtResult); 632 pResult.doLayout(); 633 showResultInText(); 634 } else if (s.equals("AutoCommit on")) { 635 try { 636 cConn.setAutoCommit(true); 637 } catch (SQLException e) {} 638 } else if (s.equals("AutoCommit off")) { 639 try { 640 cConn.setAutoCommit(false); 641 } catch (SQLException e) {} 642 } else if (s.equals("Enlarge Tree")) { 643 Dimension d = tTree.getMinimumSize(); 644 645 d.width += 20; 646 647 tTree.setMinimumSize(d); 648 fMain.pack(); 649 } else if (s.equals("Shrink Tree")) { 650 Dimension d = tTree.getMinimumSize(); 651 652 d.width -= 20; 653 654 if (d.width >= 0) { 655 tTree.setMinimumSize(d); 656 } 657 658 fMain.pack(); 659 } else if (s.equals("Enlarge Command")) { 660 txtCommand.setRows(txtCommand.getRows() + 1); 661 fMain.pack(); 662 } else if (s.equals("Shrink Command")) { 663 int i = txtCommand.getRows() - 1; 664 665 txtCommand.setRows(i < 1 ? 1 666 : i); 667 fMain.pack(); 668 } else if (s.equals("Commit")) { 669 try { 670 cConn.commit(); 671 } catch (SQLException e) {} 672 } else if (s.equals("Insert test data")) { 673 insertTestData(); 674 } else if (s.equals("Rollback")) { 675 try { 676 cConn.rollback(); 677 } catch (SQLException e) {} 678 } else if (s.equals("Disable MaxRows")) { 679 try { 680 sStatement.setMaxRows(0); 681 } catch (SQLException e) {} 682 } else if (s.equals("Set MaxRows to 100")) { 683 try { 684 sStatement.setMaxRows(100); 685 } catch (SQLException e) {} 686 } else if (s.equals("SELECT")) { 687 showHelp(DatabaseManagerCommon.selectHelp); 688 } else if (s.equals("INSERT")) { 689 showHelp(DatabaseManagerCommon.insertHelp); 690 } else if (s.equals("UPDATE")) { 691 showHelp(DatabaseManagerCommon.updateHelp); 692 } else if (s.equals("DELETE")) { 693 showHelp(DatabaseManagerCommon.deleteHelp); 694 } else if (s.equals("CREATE TABLE")) { 695 showHelp(DatabaseManagerCommon.createTableHelp); 696 } else if (s.equals("DROP TABLE")) { 697 showHelp(DatabaseManagerCommon.dropTableHelp); 698 } else if (s.equals("CREATE INDEX")) { 699 showHelp(DatabaseManagerCommon.createIndexHelp); 700 } else if (s.equals("DROP INDEX")) { 701 showHelp(DatabaseManagerCommon.dropIndexHelp); 702 } else if (s.equals("CHECKPOINT")) { 703 showHelp(DatabaseManagerCommon.checkpointHelp); 704 } else if (s.equals("SCRIPT")) { 705 showHelp(DatabaseManagerCommon.scriptHelp); 706 } else if (s.equals("SHUTDOWN")) { 707 showHelp(DatabaseManagerCommon.shutdownHelp); 708 } else if (s.equals("SET")) { 709 showHelp(DatabaseManagerCommon.setHelp); 710 } else if (s.equals("Test Script")) { 711 showHelp(DatabaseManagerCommon.testHelp); 712 } 713 } 714 715 722 void showHelp(String [] help) { 723 724 txtCommand.setText(help[0]); 725 txtResult.setText(help[1]); 726 727 bHelp = true; 728 729 pResult.removeAll(); 730 pResult.add("Center", txtResult); 731 pResult.doLayout(); 732 txtCommand.requestFocus(); 733 txtCommand.setCaretPosition(help[0].length()); 734 } 735 736 742 public void windowActivated(WindowEvent e) {} 743 744 750 public void windowDeactivated(WindowEvent e) {} 751 752 758 public void windowClosed(WindowEvent e) {} 759 760 766 public void windowClosing(WindowEvent ev) { 767 768 try { 769 cConn.close(); 770 } catch (Exception e) {} 771 772 fMain.dispose(); 773 774 if (bMustExit) { 775 System.exit(0); 776 } 777 } 778 779 785 public void windowDeiconified(WindowEvent e) {} 786 787 793 public void windowIconified(WindowEvent e) {} 794 795 801 public void windowOpened(WindowEvent e) {} 802 803 807 void clear() { 808 809 ifHuge = ""; 810 811 txtCommand.setText(ifHuge); 812 } 813 814 818 void execute() { 819 820 String sCmd = null; 821 822 if (4096 <= ifHuge.length()) { 823 sCmd = ifHuge; 824 } else { 825 sCmd = txtCommand.getText(); 826 } 827 828 if (sCmd.startsWith("-->>>TEST<<<--")) { 829 testPerformance(); 830 831 return; 832 } 833 834 String [] g = new String [1]; 835 836 lTime = System.currentTimeMillis(); 837 838 try { 839 sStatement.execute(sCmd); 840 841 lTime = System.currentTimeMillis() - lTime; 842 843 int r = sStatement.getUpdateCount(); 844 845 if (r == -1) { 846 formatResultSet(sStatement.getResultSet()); 847 } else { 848 g[0] = "update count"; 849 850 gResult.setHead(g); 851 852 g[0] = String.valueOf(r); 853 854 gResult.addRow(g); 855 } 856 857 addToRecent(txtCommand.getText()); 858 } catch (SQLException e) { 859 lTime = System.currentTimeMillis() - lTime; 860 g[0] = "SQL Error"; 861 862 gResult.setHead(g); 863 864 String s = e.getMessage(); 865 866 s += " / Error Code: " + e.getErrorCode(); 867 s += " / State: " + e.getSQLState(); 868 g[0] = s; 869 870 gResult.addRow(g); 871 } 872 873 updateResult(); 874 System.gc(); 875 } 876 877 881 void updateResult() { 882 883 if (iResult == 0) { 884 885 if (bHelp) { 887 pResult.removeAll(); 888 pResult.add("Center", gResult); 889 pResult.doLayout(); 890 891 bHelp = false; 892 } 893 894 gResult.update(); 895 gResult.repaint(); 896 } else { 897 showResultInText(); 898 } 899 900 txtCommand.selectAll(); 901 txtCommand.requestFocus(); 902 } 903 904 910 void formatResultSet(ResultSet r) { 911 912 if (r == null) { 913 String [] g = new String [1]; 914 915 g[0] = "Result"; 916 917 gResult.setHead(g); 918 919 g[0] = "(empty)"; 920 921 gResult.addRow(g); 922 923 return; 924 } 925 926 try { 927 ResultSetMetaData m = r.getMetaData(); 928 int col = m.getColumnCount(); 929 String [] h = new String [col]; 930 931 for (int i = 1; i <= col; i++) { 932 h[i - 1] = m.getColumnLabel(i); 933 } 934 935 gResult.setHead(h); 936 937 while (r.next()) { 938 for (int i = 1; i <= col; i++) { 939 try { 940 h[i - 1] = r.getString(i); 941 942 if (r.wasNull()) { 943 h[i - 1] = "(null)"; 944 } 945 } catch (SQLException e) {} 946 } 947 948 gResult.addRow(h); 949 } 950 951 r.close(); 952 } catch (SQLException e) {} 953 } 954 955 959 void testPerformance() { 960 961 String all = txtCommand.getText(); 962 StringBuffer b = new StringBuffer (); 963 long total = 0; 964 965 for (int i = 0; i < all.length(); i++) { 966 char c = all.charAt(i); 967 968 if (c != '\n') { 969 b.append(c); 970 } 971 } 972 973 all = b.toString(); 974 975 String [] g = new String [4]; 976 977 g[0] = "ms"; 978 g[1] = "count"; 979 g[2] = "sql"; 980 g[3] = "error"; 981 982 gResult.setHead(g); 983 984 int max = 1; 985 986 lTime = System.currentTimeMillis() - lTime; 987 988 while (!all.equals("")) { 989 int i = all.indexOf(';'); 990 String sql; 991 992 if (i != -1) { 993 sql = all.substring(0, i); 994 all = all.substring(i + 1); 995 } else { 996 sql = all; 997 all = ""; 998 } 999 1000 if (sql.startsWith("--#")) { 1001 max = Integer.parseInt(sql.substring(3)); 1002 1003 continue; 1004 } else if (sql.startsWith("--")) { 1005 continue; 1006 } 1007 1008 g[2] = sql; 1009 1010 long l = 0; 1011 1012 try { 1013 l = DatabaseManagerCommon.testStatement(sStatement, sql, max); 1014 total += l; 1015 g[0] = String.valueOf(l); 1016 g[1] = String.valueOf(max); 1017 g[3] = ""; 1018 } catch (SQLException e) { 1019 g[0] = g[1] = "n/a"; 1020 g[3] = e.toString(); 1021 } 1022 1023 gResult.addRow(g); 1024 System.out.println(l + " ms : " + sql); 1025 } 1026 1027 g[0] = "" + total; 1028 g[1] = "total"; 1029 g[2] = ""; 1030 1031 gResult.addRow(g); 1032 1033 lTime = System.currentTimeMillis() - lTime; 1034 1035 updateResult(); 1036 } 1037 1038 void saveAsCsv(String filename) { 1039 1040 try { 1041 File file = new File (filename); 1042 CSVWriter writer = new CSVWriter(file, null); 1043 String [] col = gResult.getHead(); 1044 int width = col.length; 1045 Vector data = gResult.getData(); 1046 String [] row; 1047 int height = data.size(); 1048 1049 writer.writeHeader(col); 1050 1051 for (int i = 0; i < height; i++) { 1052 row = (String []) data.elementAt(i); 1053 1054 String [] myRow = new String [row.length]; 1055 1056 for (int j = 0; j < row.length; j++) { 1057 String r = row[j]; 1058 1059 if (r.equals("(null)")) { 1060 1061 r = ""; 1063 } 1064 1065 myRow[j] = r; 1066 } 1067 1068 writer.writeData(myRow); 1069 } 1070 1071 writer.close(); 1072 } catch (IOException e) { 1073 throw new RuntimeException ("IOError: " + e.getMessage()); 1074 } 1075 } 1076 1077 1081 void showResultInText() { 1082 1083 String [] col = gResult.getHead(); 1084 int width = col.length; 1085 int[] size = new int[width]; 1086 Vector data = gResult.getData(); 1087 String [] row; 1088 int height = data.size(); 1089 1090 for (int i = 0; i < width; i++) { 1091 size[i] = col[i].length(); 1092 } 1093 1094 for (int i = 0; i < height; i++) { 1095 row = (String []) data.elementAt(i); 1096 1097 for (int j = 0; j < width; j++) { 1098 int l = row[j].length(); 1099 1100 if (l > size[j]) { 1101 size[j] = l; 1102 } 1103 } 1104 } 1105 1106 StringBuffer b = new StringBuffer (); 1107 1108 for (int i = 0; i < width; i++) { 1109 b.append(col[i]); 1110 1111 for (int l = col[i].length(); l <= size[i]; l++) { 1112 b.append(' '); 1113 } 1114 } 1115 1116 b.append(NL); 1117 1118 for (int i = 0; i < width; i++) { 1119 for (int l = 0; l < size[i]; l++) { 1120 b.append('-'); 1121 } 1122 1123 b.append(' '); 1124 } 1125 1126 b.append(NL); 1127 1128 for (int i = 0; i < height; i++) { 1129 row = (String []) data.elementAt(i); 1130 1131 for (int j = 0; j < width; j++) { 1132 b.append(row[j]); 1133 1134 for (int l = row[j].length(); l <= size[j]; l++) { 1135 b.append(' '); 1136 } 1137 } 1138 1139 b.append(NL); 1140 } 1141 1142 b.append(NL + height + " row(s) in " + lTime + " ms"); 1143 txtResult.setText(b.toString()); 1144 } 1145 1146 1152 private void addToRecent(String s) { 1153 1154 for (int i = 0; i < iMaxRecent; i++) { 1155 if (s.equals(sRecent[i])) { 1156 return; 1157 } 1158 } 1159 1160 if (sRecent[iRecent] != null) { 1161 mRecent.remove(iRecent); 1162 } 1163 1164 sRecent[iRecent] = s; 1165 1166 if (s.length() > 43) { 1167 s = s.substring(0, 40) + "..."; 1168 } 1169 1170 MenuItem item = new MenuItem (s); 1171 1172 item.setActionCommand("#" + iRecent); 1173 item.addActionListener(this); 1174 mRecent.insert(item, iRecent); 1175 1176 iRecent = (iRecent + 1) % iMaxRecent; 1177 } 1178 1179 1183 private void initGUI() { 1184 1185 Panel pQuery = new Panel (); 1186 Panel pCommand = new Panel (); 1187 1188 pResult = new Panel (); 1189 1190 pQuery.setLayout(new BorderLayout ()); 1191 pCommand.setLayout(new BorderLayout ()); 1192 pResult.setLayout(new BorderLayout ()); 1193 1194 Font fFont = new Font ("Dialog", Font.PLAIN, 12); 1195 1196 txtCommand = new TextArea (5, 40); 1197 1198 txtCommand.addKeyListener(this); 1199 1200 txtResult = new TextArea (20, 40); 1201 1202 txtCommand.setFont(fFont); 1203 txtResult.setFont(new Font ("Courier", Font.PLAIN, 12)); 1204 1205 butExecute = new Button ("Execute"); 1206 butClear = new Button ("Clear"); 1207 1208 butExecute.addActionListener(this); 1209 butClear.addActionListener(this); 1210 pCommand.add("East", butExecute); 1211 pCommand.add("West", butClear); 1212 pCommand.add("Center", txtCommand); 1213 1214 gResult = new Grid(); 1215 1216 setLayout(new BorderLayout ()); 1217 pResult.add("Center", gResult); 1218 pQuery.add("North", pCommand); 1219 pQuery.add("Center", pResult); 1220 fMain.add("Center", pQuery); 1221 1222 tTree = new Tree(); 1223 1224 Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); 1226 1227 if (d.width >= 640) { 1228 tTree.setMinimumSize(new Dimension (200, 100)); 1229 } else { 1230 tTree.setMinimumSize(new Dimension (80, 100)); 1231 } 1232 1233 gResult.setMinimumSize(new Dimension (200, 300)); 1234 fMain.add("West", tTree); 1235 doLayout(); 1236 fMain.pack(); 1237 } 1238 1239 1243 protected void refreshTree() { 1244 1245 tTree.removeAll(); 1246 1247 try { 1248 int color_table = Color.yellow.getRGB(); 1249 int color_column = Color.orange.getRGB(); 1250 int color_index = Color.red.getRGB(); 1251 1252 tTree.addRow("", dMeta.getURL(), "-", 0); 1253 1254 String [] usertables = { 1255 "TABLE", "GLOBAL TEMPORARY", "VIEW" 1256 }; 1257 1258 Vector schemas = new Vector (); 1260 Vector tables = new Vector (); 1261 1262 Vector remarks = new Vector (); 1264 ResultSet result = dMeta.getTables(null, null, null, usertables); 1265 1266 try { 1267 while (result.next()) { 1268 schemas.addElement(result.getString(2)); 1269 tables.addElement(result.getString(3)); 1270 remarks.addElement(result.getString(5)); 1271 } 1272 } finally { 1273 result.close(); 1274 } 1275 1276 for (int i = 0; i < tables.size(); i++) { 1277 String name = (String ) tables.elementAt(i); 1278 String schema = (String ) schemas.elementAt(i); 1279 String key = "tab-" + name + "-"; 1280 1281 tTree.addRow(key, name, "+", color_table); 1282 1283 String remark = (String ) remarks.elementAt(i); 1285 1286 if ((schema != null) &&!schema.trim().equals("")) { 1287 tTree.addRow(key + "s", "schema: " + schema); 1288 } 1289 1290 if ((remark != null) &&!remark.trim().equals("")) { 1291 tTree.addRow(key + "r", " " + remark); 1292 } 1293 1294 ResultSet col = dMeta.getColumns(null, schema, name, null); 1295 1296 try { 1297 while (col.next()) { 1298 String c = col.getString(4); 1299 String k1 = key + "col-" + c + "-"; 1300 1301 tTree.addRow(k1, c, "+", color_column); 1302 1303 String type = col.getString(6); 1304 1305 tTree.addRow(k1 + "t", "Type: " + type); 1306 1307 boolean nullable = col.getInt(11) 1308 != DatabaseMetaData.columnNoNulls; 1309 1310 tTree.addRow(k1 + "n", "Nullable: " + nullable); 1311 } 1312 } finally { 1313 col.close(); 1314 } 1315 1316 tTree.addRow(key + "ind", "Indices", "+", 0); 1317 1318 ResultSet ind = dMeta.getIndexInfo(null, schema, name, false, 1319 false); 1320 String oldiname = null; 1321 1322 try { 1323 while (ind.next()) { 1324 boolean nonunique = ind.getBoolean(4); 1325 String iname = ind.getString(6); 1326 String k2 = key + "ind-" + iname + "-"; 1327 1328 if ((oldiname == null ||!oldiname.equals(iname))) { 1329 tTree.addRow(k2, iname, "+", color_index); 1330 tTree.addRow(k2 + "u", "Unique: " + !nonunique); 1331 1332 oldiname = iname; 1333 } 1334 1335 String c = ind.getString(9); 1336 1337 tTree.addRow(k2 + "c-" + c + "-", c); 1338 } 1339 } finally { 1340 ind.close(); 1341 } 1342 } 1343 1344 tTree.addRow("p", "Properties", "+", 0); 1345 tTree.addRow("pu", "User: " + dMeta.getUserName()); 1346 tTree.addRow("pr", "ReadOnly: " + cConn.isReadOnly()); 1347 tTree.addRow("pa", "AutoCommit: " + cConn.getAutoCommit()); 1348 tTree.addRow("pd", "Driver: " + dMeta.getDriverName()); 1349 tTree.addRow("pp", "Product: " + dMeta.getDatabaseProductName()); 1350 tTree.addRow("pv", 1351 "Version: " + dMeta.getDatabaseProductVersion()); 1352 } catch (SQLException e) { 1353 tTree.addRow("", "Error getting metadata:", "-", 0); 1354 tTree.addRow("-", e.getMessage()); 1355 tTree.addRow("-", e.getSQLState()); 1356 } 1357 1358 tTree.update(); 1359 } 1360} 1361 | Popular Tags |