📄 databasemanager.java
字号:
/* Copyrights and Licenses * * This product includes Hypersonic SQL. * Originally developed by Thomas Mueller and the Hypersonic SQL Group. * * Copyright (c) 1995-2000 by the Hypersonic SQL Group. All rights reserved. * Redistribution and use in source and binary forms, with or without modification, are permitted * provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, this list of conditions * and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * - All advertising materials mentioning features or use of this software must display the * following acknowledgment: "This product includes Hypersonic SQL." * - Products derived from this software may not be called "Hypersonic SQL" nor may * "Hypersonic SQL" appear in their names without prior written permission of the * Hypersonic SQL Group. * - Redistributions of any form whatsoever must retain the following acknowledgment: "This * product includes Hypersonic SQL." * This software is provided "as is" and any expressed or implied warranties, including, but * not limited to, the implied warranties of merchantability and fitness for a particular purpose are * disclaimed. In no event shall the Hypersonic SQL Group or its contributors be liable for any * direct, indirect, incidental, special, exemplary, or consequential damages (including, but * not limited to, procurement of substitute goods or services; loss of use, data, or profits; * or business interruption). However caused any on any theory of liability, whether in contract, * strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this * software, even if advised of the possibility of such damage. * This software consists of voluntary contributions made by many individuals on behalf of the * Hypersonic SQL Group. * * * For work added by the HSQL Development Group: * * Copyright (c) 2001-2002, The HSQL Development Group * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer, including earlier * license statements (above) and comply with all above license conditions. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution, including earlier * license statements (above) and comply with all above license conditions. * * Neither the name of the HSQL Development Group nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package org.hsqldb.util;import java.awt.*;import java.awt.event.*;import java.awt.image.*;import java.applet.*;import java.sql.*;import java.io.File;import java.util.*;// sqlbob@users 20020401 - patch 1.7.0 by sqlbob (RMP) - enhancements// sqlbob@users 20020401 - patch 537501 by ulrivo - command line arguments// sqlbob@users 20020407 - patch 1.7.0 - reengineering// nickferguson@users 20021005 - patch 1.7.1 - enhancements/** * AWT Tool for manageing a JDBC database.<p> * <pre> * Usage: java DatabaseManagerSwing [-options] * where options include: * -driver <classname> jdbc driver class * -url <name> jdbc url * -user <name> username used for connection * -password <password> password for this user * -dir <path> default directory * -script <file> reads from script file *</pre> * @version 1.7.0 */public class DatabaseManager extends Appletimplements ActionListener, WindowListener, KeyListener { final static String NL = System.getProperty("line.separator"); final static int iMaxRecent = 24; Connection cConn; DatabaseMetaData dMeta; Statement sStatement; Menu mRecent; String sRecent[]; int iRecent; TextArea txtCommand; Button butExecute; Button butClear; Tree tTree; Panel pResult; long lTime; int iResult; // 0: grid; 1: text Grid gResult; TextArea txtResult; boolean bHelp; Frame fMain; Image imgEmpty; static boolean bMustExit; String ifHuge = ""; // (ulrivo): variables set by arguments from the commandline static String defDriver = "org.hsqldb.jdbcDriver"; static String defURL = "jdbc:hsqldb:."; static String defUser = "sa"; static String defPassword = ""; static String defScript; static String defDirectory; /** * Method declaration * * * @param c */ void connect(Connection c) { if (c == null) { return; } if (cConn != null) { try { cConn.close(); } catch (SQLException e) {} } cConn = c; try { dMeta = cConn.getMetaData(); sStatement = cConn.createStatement(); refreshTree(); } catch (SQLException e) { e.printStackTrace(); } } /** * Method declaration * */ public void init() { DatabaseManager m = new DatabaseManager(); m.main(); try { m.connect(ConnectionDialog.createConnection(defDriver, defURL, defUser, defPassword)); m.insertTestData(); m.refreshTree(); } catch (Exception e) { e.printStackTrace(); } } /** * Method declaration * * * @param arg */ public static void main(String arg[]) { System.getProperties().put("sun.java2d.noddraw", "true"); // (ulrivo): read all arguments from the command line String lowerArg; boolean autoConnect = false; for (int i = 0; i < arg.length; i++) { lowerArg = arg[i].toLowerCase(); i++; if (i == arg.length) { showUsage(); return; } if (lowerArg.equals("-driver")) { defDriver = arg[i]; autoConnect = true; } else if (lowerArg.equals("-url")) { defURL = arg[i]; autoConnect = true; } else if (lowerArg.equals("-user")) { defUser = arg[i]; autoConnect = true; } else if (lowerArg.equals("-password")) { defPassword = arg[i]; autoConnect = true; } else if (lowerArg.equals("-dir")) { defDirectory = arg[i]; } else if (lowerArg.equals("-script")) { defScript = arg[i]; } else { showUsage(); return; } } bMustExit = true; DatabaseManager m = new DatabaseManager(); m.main(); Connection c = null; try { if (autoConnect) { c = ConnectionDialog.createConnection(defDriver, defURL, defUser, defPassword); } else { c = ConnectionDialog.createConnection(m.fMain, "Connect"); } } catch (Exception e) { e.printStackTrace(); } if (c == null) { return; } m.connect(c); } private static void showUsage() { System.out.println( "Usage: java DatabaseManager [-options]\n" + "where options include:\n" + " -driver <classname> jdbc driver class\n" + " -url <name> jdbc url\n" + " -user <name> username used for connection\n" + " -password <password> password for this user\n" + " -dir <path> default directory\n" + " -script <file> reads from script file\n"); } /** * Method declaration * */ void insertTestData() { try { DatabaseManagerCommon.createTestTables(sStatement); refreshTree(); txtCommand.setText( DatabaseManagerCommon.createTestData(sStatement)); refreshTree(); for (int i = 0; i < DatabaseManagerCommon.testDataSql.length; i++) { addToRecent(DatabaseManagerCommon.testDataSql[i]); } execute(); } catch (SQLException e) { e.printStackTrace(); } } /** * Method declaration * */ void main() { fMain = new Frame("HSQL Database Manager"); imgEmpty = createImage(new MemoryImageSource(2, 2, new int[4 * 4], 2, 2)); fMain.setIconImage(imgEmpty); fMain.addWindowListener(this); MenuBar bar = new MenuBar(); // used shortcuts: CERGTSIUDOLM String fitems[] = { "-Connect...", "--", "-Open Script...", "-Save Script...", "-Save Result...", "--", "-Exit" }; addMenu(bar, "File", fitems); String vitems[] = { "RRefresh Tree", "--", "GResults in Grid", "TResults in Text", "--", "1Shrink Tree", "2Enlarge Tree", "3Shrink Command", "4Enlarge Command" }; addMenu(bar, "View", vitems); String sitems[] = { "SSELECT", "IINSERT", "UUPDATE", "DDELETE", "--", "-CREATE TABLE", "-DROP TABLE", "-CREATE INDEX", "-DROP INDEX", "--", "-CHECKPOINT", "-SCRIPT", "-SET", "-SHUTDOWN", "--", "-Test Script" }; addMenu(bar, "Command", sitems); Menu recent = new Menu("Recent"); mRecent = new Menu("Recent"); bar.add(mRecent); String soptions[] = { "-AutoCommit on", "-AutoCommit off", "OCommit", "LRollback", "--", "-Disable MaxRows", "-Set MaxRows to 100", "--", "-Logging on", "-Logging off", "--", "-Insert test data" }; addMenu(bar, "Options", soptions); String stools[] = { "-Dump", "-Restore", "-Transfer" }; addMenu(bar, "Tools", stools); fMain.setMenuBar(bar); fMain.setSize(640, 480); fMain.add("Center", this); initGUI(); sRecent = new String[iMaxRecent]; Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); Dimension size = fMain.getSize(); // (ulrivo): full size on screen with less than 640 width if (d.width >= 640) { fMain.setLocation((d.width - size.width) / 2, (d.height - size.height) / 2); } else { fMain.setLocation(0, 0); fMain.setSize(d); } fMain.show(); // (ulrivo): load query from command line if (defScript != null) { if (defDirectory != null) { defScript = defDirectory + File.separator + defScript; } txtCommand.setText(DatabaseManagerCommon.readFile(defScript)); } txtCommand.requestFocus(); } /** * Method declaration * * * @param b * @param name * @param items */ void addMenu(MenuBar b, String name, String items[]) { Menu menu = new Menu(name); addMenuItems(menu, items); b.add(menu); } /** * Method declaration * * * @param f * @param m */ void addMenuItems(Menu f, String m[]) { for (int i = 0; i < m.length; i++) { MenuItem item = new MenuItem(m[i].substring(1)); char c = m[i].charAt(0); if (c != '-') { item.setShortcut(new MenuShortcut(c));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -