| 1 65 66 67 package org.hsqldb.util; 68 69 import java.io.File ; 70 import java.io.FileInputStream ; 71 import java.io.FileOutputStream ; 72 import java.io.IOException ; 73 import java.lang.reflect.Constructor ; 74 import java.lang.reflect.InvocationTargetException ; 75 import java.sql.Connection ; 76 import java.sql.DatabaseMetaData ; 77 import java.sql.Driver ; 78 import java.sql.DriverManager ; 79 import java.sql.ResultSet ; 80 import java.sql.ResultSetMetaData ; 81 import java.sql.SQLException ; 82 import java.sql.Statement ; 83 import java.text.DecimalFormat ; 84 import java.util.ArrayList ; 85 import java.util.HashMap ; 86 import java.util.HashSet ; 87 import java.util.Hashtable ; 88 import java.util.Iterator ; 89 import java.util.Locale ; 90 import java.util.Properties ; 91 import java.util.Vector ; 92 import java.awt.BorderLayout ; 93 import java.awt.Cursor ; 94 import java.awt.Dimension ; 95 import java.awt.Event ; 96 import java.awt.Font ; 97 import java.awt.Insets ; 98 import java.awt.Toolkit ; 99 import java.awt.event.ActionEvent ; 100 import java.awt.event.ActionListener ; 101 import java.awt.event.KeyEvent ; 102 import java.awt.event.KeyListener ; 103 import java.awt.event.WindowEvent ; 104 import java.awt.event.WindowListener ; 105 import java.awt.Component ; 106 import java.awt.Container ; 107 108 import javax.swing.ButtonGroup ; 109 import javax.swing.ImageIcon ; 110 import javax.swing.JApplet ; 111 import javax.swing.JButton ; 112 import javax.swing.JCheckBoxMenuItem ; 113 import javax.swing.JComponent ; 114 import javax.swing.JFileChooser ; 115 import javax.swing.JFrame ; 116 import javax.swing.JLabel ; 117 import javax.swing.JMenu ; 118 import javax.swing.JMenuBar ; 119 import javax.swing.JMenuItem ; 120 import javax.swing.JOptionPane ; 121 import javax.swing.JPanel ; 122 import javax.swing.JRadioButtonMenuItem ; 123 import javax.swing.JScrollPane ; 124 import javax.swing.JSplitPane ; 125 import javax.swing.JTable ; 126 import javax.swing.JTextArea ; 127 import javax.swing.JToolBar ; 128 import javax.swing.JTree ; 129 import javax.swing.KeyStroke ; 130 import javax.swing.SwingUtilities ; 131 import javax.swing.table.TableModel ; 132 import javax.swing.tree.DefaultMutableTreeNode ; 133 import javax.swing.tree.DefaultTreeModel ; 134 import javax.swing.tree.MutableTreeNode ; 135 import javax.swing.RootPaneContainer ; 136 137 import java.security.AccessControlException ; 138 139 import org.hsqldb.lib.java.JavaSystem; 140 141 162 187 public class DatabaseManagerSwing extends JApplet  188 implements ActionListener , WindowListener , KeyListener { 189 190 199 private static String homedir = null; 200 201 static { 202 try { 203 Class c = Class.forName("sun.security.action.GetPropertyAction"); 204 Constructor constructor = c.getConstructor(new Class []{ 205 String .class }); 206 java.security.PrivilegedAction a = 207 (java.security.PrivilegedAction ) constructor.newInstance( 208 new Object []{ "user.home" }); 209 210 homedir = (String ) java.security.AccessController.doPrivileged(a); 211 } catch (IllegalAccessException e) { 212 System.err.println( 213 "Failed to get home directory.\n" 214 + "Therefore not retrieving/storing user preferences.\n(" 215 + e.getMessage() + ')'); 216 } catch (NoSuchMethodException e) { 217 System.err.println( 218 "Failed to get home directory.\n" 219 + "Therefore not retrieving/storing user preferences.\n(" 220 + e.getMessage() + ')'); 221 } catch (ClassNotFoundException e) { 222 System.err.println( 223 "Failed to get home directory.\n" 224 + "Therefore not retrieving/storing user preferences.\n(" 225 + e.getMessage() + ')'); 226 } catch (InstantiationException e) { 227 System.err.println( 228 "Failed to get home directory.\n" 229 + "Therefore not retrieving/storing user preferences.\n(" 230 + e.getMessage() + ')'); 231 } catch (InvocationTargetException e) { 232 System.err.println( 233 "Failed to get home directory.\n" 234 + "Therefore not retrieving/storing user preferences.\n(" 235 + e.getMessage() + ')'); 236 } catch (AccessControlException e) { 237 System.err.println( 238 "Failed to get home directory.\n" 239 + "Therefore not retrieving/storing user preferences.\n(" 240 + e.getMessage() + ')'); 241 } 242 } 243 244 ArrayList localActionList = new ArrayList (); 245 private JFrame jframe = null; 246 private static final String DEFAULT_RCFILE = homedir + "/dbmanager.rc"; 247 private static final String HELP_TEXT = 248 "See the forums, mailing lists, and HSQLDB User Guide\n" 249 + "at http://hsqldb.org.\n\n" 250 + "Please paste the following version identifier with any\n" 251 + "problem reports or help requests: $Revision: 1.69 $"; 252 private static final String ABOUT_TEXT = 253 "$Revision: 1.69 $ of DatabaseManagerSwing\n\n" 254 + "Copyright (c) 1995-2000, The Hypersonic SQL Group.\n" 255 + "Copyright (c) 2001-2005, The HSQL Development Group.\n" 256 + "http://hsqldb.org\n\n\n" 257 + "You may use and redistribute according to the HSQLDB\n" 258 + "license documented in the source code and at the web\n" 259 + "site above."; 260 static final String NL = System.getProperty("line.separator"); 261 static final String NULL_STR = "[null]"; 262 static int iMaxRecent = 24; 263 Connection cConn; 264 Connection rowConn; DatabaseMetaData dMeta; 266 Statement sStatement; 267 JMenu mRecent; 268 String [] sRecent; 269 int iRecent; 270 JTextArea txtCommand; 271 JScrollPane txtCommandScroll; 272 JButton butExecute; 273 JTree tTree; 274 JScrollPane tScrollPane; 275 DefaultTreeModel treeModel; 276 TableModel tableModel; 277 DefaultMutableTreeNode rootNode; 278 JPanel pResult; 279 long lTime; 280 GridSwing gResult; 281 282 290 JTable gResultTable; 291 JScrollPane gScrollPane; 292 JTextArea txtResult; 293 JScrollPane txtResultScroll; 294 JSplitPane nsSplitPane; JSplitPane ewSplitPane; boolean bHelp; 297 RootPaneContainer fMain; 298 static boolean bMustExit; 299 300 301 String sqlScriptBuffer = null; 302 JToolBar jtoolbar; 303 private boolean showSchemas = true; 304 private boolean showTooltips = true; 305 private boolean autoRefresh = true; 306 private boolean gridFormat = true; 307 308 static DatabaseManagerSwing refForFontDialogSwing; 310 boolean displayRowCounts = false; 311 boolean showSys = false; 312 boolean showIndexDetails = true; 313 String currentLAF = null; 314 JPanel pStatus; 315 static JButton iReadyStatus; 316 JRadioButtonMenuItem rbAllSchemas = new JRadioButtonMenuItem ("*"); 317 JMenuItem mitemAbout = new JMenuItem ("About", 'A'); 318 JMenuItem mitemHelp = new JMenuItem ("Help", 'H'); 319 JMenuItem mitemUpdateSchemas = new JMenuItem ("Update Schemas"); 320 JCheckBoxMenuItem boxAutoCommit = 321 new JCheckBoxMenuItem (AUTOCOMMIT_BOX_TEXT); 322 JCheckBoxMenuItem boxLogging = new JCheckBoxMenuItem (LOGGING_BOX_TEXT); 323 JCheckBoxMenuItem boxShowSchemas = 324 new JCheckBoxMenuItem (SHOWSCHEMAS_BOX_TEXT); 325 JCheckBoxMenuItem boxAutoRefresh = 326 new JCheckBoxMenuItem (AUTOREFRESH_BOX_TEXT); 327 JCheckBoxMenuItem boxTooltips = new JCheckBoxMenuItem (SHOWTIPS_BOX_TEXT); 328 JCheckBoxMenuItem boxRowCounts = 329 new JCheckBoxMenuItem (ROWCOUNTS_BOX_TEXT); 330 JCheckBoxMenuItem boxShowGrid = new JCheckBoxMenuItem (GRID_BOX_TEXT); 331 JCheckBoxMenuItem boxShowSys = new JCheckBoxMenuItem (SHOWSYS_BOX_TEXT); 332 333 JRadioButtonMenuItem rbNativeLF = 335 new JRadioButtonMenuItem ("Native Look & Feel"); 336 JRadioButtonMenuItem rbJavaLF = 337 new JRadioButtonMenuItem ("Java Look & Feel"); 338 JRadioButtonMenuItem rbMotifLF = 339 new JRadioButtonMenuItem ("Motif Look & Feel"); 340 JLabel jStatusLine; 341 static String READY_STATUS = "Ready"; 342 private static final String AUTOCOMMIT_BOX_TEXT = "Autocommit mode"; 343 private static final String LOGGING_BOX_TEXT = "Logging mode"; 344 private static final String SHOWSCHEMAS_BOX_TEXT = "Show schemas"; 345 private static final String AUTOREFRESH_BOX_TEXT = "Auto-refresh tree"; 346 private static final String SHOWTIPS_BOX_TEXT = "Show Tooltips"; 347 private static final String ROWCOUNTS_BOX_TEXT = "Show row counts"; 348 private static final String SHOWSYS_BOX_TEXT = "Show system tables"; 349 private static final String GRID_BOX_TEXT = 350 "Show results in Grid (a.o.t. Text)"; 351 352 Cursor fMainCursor; 355 Cursor txtCommandCursor; 356 Cursor txtResultCursor; 357 HashMap tipMap = new HashMap (); 358 private JMenu mnuSchemas = new JMenu ("Schemas"); 359 360 363 364 private final Cursor waitCursor = new Cursor (Cursor.WAIT_CURSOR); 366 367 static String defDriver = "org.hsqldb.jdbcDriver"; 371 static String defURL = "jdbc:hsqldb:."; 372 static String defUser = "sa"; 373 static String defPassword = ""; 374 static String defScript; 375 static String defDirectory; 376 private String schemaFilter = null; 377 378 public DatabaseManagerSwing() { 379 jframe = new JFrame ("HSQLDB DatabaseManager"); 380 fMain = jframe; 381 } 382 ; 383 384 public DatabaseManagerSwing(JFrame frameIn) { 385 jframe = frameIn; 386 fMain = jframe; 387 } 388 ; 389 390 public void init() { 391 392 javax.swing.AbstractButton btn; 393 394 fMain = this; 395 396 main(); 397 398 for (int i = 0; i < localActionList.size(); i++) { 399 btn = (javax.swing.AbstractButton ) localActionList.get(i); 400 401 btn.setEnabled(false); 402 } 403 404 Connection c = null; 405 boolean auto = false; 406 407 if (getParameter("jdbcDriver") != null) { 408 auto = true; 409 defDriver = getParameter("jdbcDriver"); 410 } 411 412 if (getParameter("jdbcUrl") != null) { 413 auto = true; 414 defURL = getParameter("jdbcUrl"); 415 } 416 417 if (getParameter("jdbcUser") != null) { 418 auto = true; 419 defUser = getParameter("jdbcUser"); 420 } 421 422 if (getParameter("jdbcPassword") != null) { 423 auto = true; 424 defPassword = getParameter("jdbcPassword"); 425 } 426 427 try { 428 setWaiting("Initializing"); 429 430 c = (auto 433 ? ConnectionDialogSwing.createConnection(defDriver, defURL, 434 defUser, defPassword) 435 : ConnectionDialogSwing.createConnection(jframe, "Connect")); 436 } catch (Exception e) { 437 438 CommonSwing.errorMessage(e); 440 } finally { 441 setWaiting(null); 442 } 443 444 if (c != null) { 445 connect(c); 446 } 447 448 if (getParameter("loadSampleData") != null 449 && getParameter("loadSampleData").equals("true")) { 450 insertTestData(); 451 452 try { 453 Thread.sleep(1000); 454 } catch (InterruptedException ie) {} 455 ; 456 457 refreshTree(); 460 } 461 462 if (getParameter("schemaFilter") != null) { 463 schemaFilter = getParameter("schemaFilter"); 464 } 465 } 466 467 public static void main(String [] arg) { 468 469 System.getProperties().put("sun.java2d.noddraw", "true"); 470 471 String lowerArg; 473 String urlid = null; 474 String rcFile = null; 475 boolean autoConnect = false; 476 boolean urlidConnect = false; 477 478 bMustExit = true; 479 480 for (int i = 0; i < arg.length; i++) { 481 lowerArg = arg[i].toLowerCase(); 482 483 if (lowerArg.length() > 1 && lowerArg.charAt(1) == '-') { 484 lowerArg = lowerArg.substring(1); 485 } 486 487 i++; 488 489 if (lowerArg.equals("-driver")) { 490 defDriver = arg[i]; 491 autoConnect = true; 492 } else if (lowerArg.equals("-url")) { 493 defURL = arg[i]; 494 autoConnect = true; 495 } else if (lowerArg.equals("-user")) { 496 defUser = arg[i]; 497 autoConnect = true; 498 } else if (lowerArg.equals("-password")) { 499 defPassword = arg[i]; 500 autoConnect = true; 501 } else if (lowerArg.equals("-urlid")) { 502 urlid = arg[i]; 503 urlidConnect = true; 504 } else if (lowerArg.equals("-rcfile")) { 505 rcFile = arg[i]; 506 urlidConnect = true; 507 } else if (lowerArg.equals("-dir")) { 508 defDirectory = arg[i]; 509 } else if (lowerArg.equals("-script")) { 510 defScript = arg[i]; 511 } else if (lowerArg.equals("-noexit")) { 512 bMustExit = false; 513 514 i--; 515 } else { 516 showUsage(); 517 518 return; 519 } 520 } 521 522 DatabaseManagerSwing m = 523 new DatabaseManagerSwing(new JFrame ("HSQL Database Manager")); 524 525 refForFontDialogSwing = m; 527 528 m.main(); 529 530 Connection c = null; 531 532 m.setWaiting("Initializing"); 533 534 try { 535 if (autoConnect && urlidConnect) { 536 throw new IllegalArgumentException ( 537 "You may not specify both (urlid) AND (url/user/password)."); 538 } 539 540 if (autoConnect) { 541 c = ConnectionDialogSwing.createConnection(defDriver, defURL, 542 defUser, defPassword); 543 } else if (urlidConnect) { 544 if (urlid == null) { 545 throw new IllegalArgumentException ( 546 "You must specify an 'urlid' to use an RC file"); 547 } 548 549 autoConnect = true; 550 551 String rcfilepath = (rcFile == null) ? DEFAULT_RCFILE 552 : rcFile; 553 RCData rcdata = new RCData(new File (rcfilepath), urlid); 554 555 c = rcdata.getConnection( 556 null, System.getProperty("sqlfile.charset"), 557 System.getProperty("javax.net.ssl.trustStore")); 558 } else { 559 c = ConnectionDialogSwing.createConnection(m.jframe, 560 "Connect"); 561 } 562 } catch (Exception e) { 563 564 CommonSwing.errorMessage(e); 566 } finally { 567 m.setWaiting(null); 568 } 569 570 if (c != null) { 571 m.connect(c); 572 } 573 574 FontDialogSwing.CreatFontDialog(refForFontDialogSwing); 576 m.start(); 577 } 578 579 584 public void connect(Connection c) { 585 586 schemaFilter = null; 587 588 if (c == null) { 589 return; 590 } 591 592 if (cConn != null) { 593 try { 594 cConn.close(); 595 } catch (SQLException e) { 596 597 CommonSwing.errorMessage(e); 599 } 600 } 601 602 cConn = c; 603 604 rowConn = c; 606 607 try { 608 dMeta = cConn.getMetaData(); 609 sStatement = cConn.createStatement(); 610 611 updateAutoCommitBox(); 612 613 showIndexDetails = 615 (dMeta.getDatabaseProductName().indexOf("Oracle") < 0); 616 617 Driver driver = DriverManager.getDriver(dMeta.getURL()); 618 ConnectionSetting newSetting = new ConnectionSetting( 619 dMeta.getDatabaseProductName(), driver.getClass().getName(), 620 dMeta.getURL(), 621 dMeta.getUserName().replaceAll("@localhost", ""), ""); 622 Hashtable settings = 623 ConnectionDialogCommon.loadRecentConnectionSettings(); 624 625 ConnectionDialogCommon.addToRecentConnectionSettings(settings, 626 newSetting); 627 ConnectionDialogSwing.setConnectionSetting(newSetting); 628 refreshTree(); 629 clearResultPanel(); 630 631 if (fMain instanceof JApplet ) { 632 getAppletContext().showStatus( 633 "JDBC Connection established to a " 634 + dMeta.getDatabaseProductName() + " v. " 635 + dMeta.getDatabaseProductVersion() + " database as '" 636 + dMeta.getUserName() + "'."); 637 } 638 } catch (SQLException e) { 639 640 CommonSwing.errorMessage(e); 642 } catch (IOException e) { 643 644 CommonSwing.errorMessage(e); 646 } catch (Exception e) { 647 CommonSwing.errorMessage(e); 648 } 649 } 650 651 private static void showUsage() { 652 653 System.out.println( 654 "Usage: java DatabaseManagerSwing [--options]\n" 655 + "where options include:\n" 656 + " --driver <classname> jdbc driver class\n" 657 + " --url <name> jdbc url\n" 658 + " --user <name> username used for connection\n" 659 + " --password <password> password for this user\n" 660 + " --urlid <urlid> use url/user/password/driver in rc file\n" 661 + " --rcfile <file> (defaults to 'dbmanager.rc' in home dir)\n" 662 + " --dir <path> default directory\n" 663 + " --script <file> reads from script file\n" 664 + " --noexit do not call system.exit()\n" 665 + "(Single-hypen switches like '-driver' are also supported)"); 666 } 667 668 private void insertTestData() { 669 670 try { 671 DatabaseManagerCommon.createTestTables(sStatement); 672 txtCommand.setText( 673 DatabaseManagerCommon.createTestData(sStatement)); 674 675 for (int i = 0; i < DatabaseManagerCommon.testDataSql.length; 676 i++) { 677 addToRecent(DatabaseManagerCommon.testDataSql[i]); 678 } 679 680 executeCurrentSQL(); 681 } catch (SQLException e) { 682 683 CommonSwing.errorMessage(e); 685 } 686 } 687 688 public void setMustExit(boolean b) { 689 this.bMustExit = b; 690 } 691 692 private DBMPrefs prefs = null; 693 694 public void main() { 695 696 JMenu jmenu; 697 JMenuItem mitem; 698 699 try { 700 prefs = new DBMPrefs(fMain instanceof JApplet ); 701 } catch (Exception e) { 702 System.err.println( 703 "Failed to load preferences. Proceeding with defaults:\n"); 704 } 705 706 if (prefs == null) { 707 setLF(CommonSwing.Native); 708 } else { 709 autoRefresh = prefs.autoRefresh; 710 displayRowCounts = prefs.showRowCounts; 711 showSys = prefs.showSysTables; 712 showSchemas = prefs.showSchemas; 713 gridFormat = prefs.resultGrid; 714 showTooltips = prefs.showTooltips; 715 716 setLF(prefs.laf); 717 } 718 719 fMain.getContentPane().add(createToolBar(), "North"); 721 722 if (fMain instanceof java.awt.Frame ) { 723 ((java.awt.Frame ) fMain).setIconImage( 724 CommonSwing.
|