⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 databasemanager.java

📁 hsqldb是100%java实现的数据库,是一个开放源代码的JAVA数据库 l 具有标准的SQL语法和JAVA接口 l HSQLDB可以自由使用和分发 l 非常简洁和快速的
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* Copyright (c) 2001-2005, 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. * * 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. * * 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.applet.Applet;import java.io.File;import java.io.IOException;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import java.util.Vector;import java.awt.BorderLayout;import java.awt.Button;import java.awt.Color;import java.awt.Dimension;import java.awt.FileDialog;import java.awt.Font;import java.awt.Frame;import java.awt.Image;import java.awt.Menu;import java.awt.MenuBar;import java.awt.MenuItem;import java.awt.MenuShortcut;import java.awt.Panel;import java.awt.TextArea;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.awt.image.MemoryImageSource;import org.hsqldb.lib.java.JavaSystem;// 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 *              --urlid <urlid>       get connection info from RC file *              --rcfile <file>       use instead of default (with urlid) *              --dir <path>          default directory *              --script <file>       reads from script file *</pre> * Tue Apr 26 16:38:54 EDT 2005 * Switched default switch method from "-switch" to "--switch" because * "-switch" usage is ambiguous as used here.  Single switches should * be reserved for single-letter switches which can be mixed like * "-u -r -l" = "-url".  -blaine * * @author Thomas Mueller (Hypersonic SQL Group) * @version 1.8.0 * @since Hypersonic SQL */public class DatabaseManager extends Appletimplements ActionListener, WindowListener, KeyListener {    private static final String DEFAULT_RCFILE =        System.getProperty("user.home") + "/dbmanager.rc";    static final String NL         = System.getProperty("line.separator");    static final 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     */    public 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;        String  urlid        = null;        String  rcFile       = null;        boolean autoConnect  = false;        boolean urlidConnect = false;        bMustExit = true;        for (int i = 0; i < arg.length; i++) {            lowerArg = arg[i].toLowerCase();            if (lowerArg.length() > 1 && lowerArg.charAt(1) == '-') {                lowerArg = lowerArg.substring(1);            }            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("-urlid")) {                urlid        = arg[i];                urlidConnect = true;            } else if (lowerArg.equals("-rcfile")) {                rcFile       = arg[i];                urlidConnect = true;            } else if (lowerArg.equals("-dir")) {                defDirectory = arg[i];            } else if (lowerArg.equals("-script")) {                defScript = arg[i];            } else if (lowerArg.equals("-noexit")) {                bMustExit = false;                i--;            } else {                showUsage();                return;            }        }        DatabaseManager m = new DatabaseManager();        m.main();        Connection c = null;        try {            if (autoConnect && urlidConnect) {                throw new IllegalArgumentException(                    "You may not specify both (urlid) AND (url/user/password).");            }            if (autoConnect) {                c = ConnectionDialog.createConnection(defDriver, defURL,                                                      defUser, defPassword);            } else if (urlidConnect) {                if (urlid == null) {                    throw new IllegalArgumentException(                        "You must specify an 'urlid' to use an RC file");                }                autoConnect = true;                c = (new RCData(new File((rcFile == null) ? DEFAULT_RCFILE                                                          : rcFile), urlid).getConnection(                                                          null, System.getProperty(                                                              "sqlfile.charset"), System.getProperty(                                                                  "javax.net.ssl.trustStore")));            } 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"            + "    --urlid <urlid>       use url/user/password/driver in rc file\n"            + "    --rcfile <file>       (defaults to 'dbmanager.rc' in home dir)\n"            + "    --dir <path>          default directory\n"            + "    --script <file>       reads from script file\n"            + "    --noexit              do not call system.exit()\n"            + "(Single-hypen switches like '-driver' are also supported)");    }    /**     * 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     *     */    public 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...", "-Save Result csv...", "--", "-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 != '-') {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -