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

📄 sqlviewer.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    for (i = 0; i < s.length(); i++) {
      if (s.charAt(i) == '"') {
        quote = !quote;
        tmpStr += "" + s.charAt(i);
      }
      else if (s.charAt(i) == ',') {
        if (quote) {
          tmpStr += "" + s.charAt(i);
        }
        else {
          if (tmpStr.startsWith("\""))
            tmpStr = tmpStr.substring(1, tmpStr.length() - 1);
          result.addElement(tmpStr);
          tmpStr = "";
        }
      }
      else {
        tmpStr += "" + s.charAt(i);
      }
    }
    
    // add last element
    if (!tmpStr.equals("")) {
      if (tmpStr.startsWith("\""))
        tmpStr = tmpStr.substring(1, tmpStr.length() - 1);
      result.addElement(tmpStr);
    }

    return result;
  }

  /**
   * converts the given model into a comma-separated string
   * @param m       the model to convert
   * @return        the string representation of the model
   */
  protected String modelToString(DefaultListModel m) {
    String      result;
    String      tmpStr;
    int         i;
    int         n;
    boolean     quote;

    result = "";

    for (i = 0; i < m.size(); i++) {
      if (i > 0)
        result += ",";
      
      tmpStr = m.get(i).toString();
      quote  = (tmpStr.indexOf(",") > -1) || (tmpStr.indexOf(" ") > -1);
      
      if (quote)
        result += "\"";
      
      for (n = 0; n < tmpStr.length(); n++) {
        // double quotes
        if (tmpStr.charAt(n) == '"')
          result += "" + "\"\"";
        // normal character
        else
          result += "" + tmpStr.charAt(n);
      }
      
      if (quote)
        result += "\"";
    }

    return result;
  }

  /**
   * loads the history properties of the SqlViewer in the user's home directory
   * @param set       whether to set the read properties in the panels or not
   * @see #HISTORY_FILE
   */
  protected void loadHistory(boolean set) {
    BufferedInputStream     str;
    File                    file;

    try {
      file = new File(getHistoryFilename());
      if (file.exists()) {
        str = new BufferedInputStream(
            new FileInputStream(getHistoryFilename()));
        m_History.load(str);
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    // set the histories
    if (set) {
      m_ConnectionPanel.setHistory(
          stringToModel(
            m_History.getProperty(ConnectionPanel.HISTORY_NAME, "")));
      m_QueryPanel.setHistory(
          stringToModel(
            m_History.getProperty(QueryPanel.HISTORY_NAME, "")));
      m_QueryPanel.setMaxRows(
          Integer.parseInt(m_History.getProperty(QueryPanel.MAX_ROWS, "100")));
    }
  }

  /**
   * saves the history properties of the SqlViewer in the user's home directory
   * @see #HISTORY_FILE
   */
  protected void saveHistory() {
    BufferedOutputStream    str;
    
    try {
      str = new BufferedOutputStream(
          new FileOutputStream(getHistoryFilename()));
      m_History.store(str, "SQL-Viewer-History");
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * calls the clear method of all sub-panels to set back to default values
   * and free up memory
   */
  public void clear() {
    m_ConnectionPanel.clear();
    m_QueryPanel.clear();
    m_ResultPanel.clear();
    m_InfoPanel.clear();
  }

  /**
   * returns the database URL from the currently active tab in the ResultPanel,
   * otherwise an empty string.
   * @see ResultPanel
   * @return        the currently selected tab's URL
   */
  public String getURL() {
    return m_URL;
  }

  /**
   * returns the user from the currently active tab in the ResultPanel,
   * otherwise an empty string.
   * @see ResultPanel
   * @return        the currently selected tab's user
   */
  public String getUser() {
    return m_User;
  }

  /**
   * returns the password from the currently active tab in the ResultPanel,
   * otherwise an empty string.
   * @see ResultPanel
   * @return        the currently selected tab's password
   */
  public String getPassword() {
    return m_Password;
  }

  /**
   * returns the query from the currently active tab in the ResultPanel,
   * otherwise an empty string.
   * @see ResultPanel
   * @return        the currently selected tab's query
   */
  public String getQuery() {
    return m_Query;
  }

  /**
   * adds the given listener to the list of listeners
   * @param l       the listener to add to the list
   */
  public void addConnectionListener(ConnectionListener l) {
    m_ConnectionPanel.addConnectionListener(l);
  }

  /**
   * removes the given listener from the list of listeners
   * @param l       the listener to remove
   */
  public void removeConnectionListener(ConnectionListener l) {
    m_ConnectionPanel.removeConnectionListener(l);
  }

  /**
   * adds the given listener to the list of listeners
   * @param l       the listener to add to the list
   */
  public void addQueryExecuteListener(QueryExecuteListener l) {
    m_QueryPanel.addQueryExecuteListener(l);
  }

  /**
   * removes the given listener from the list of listeners
   * @param l       the listener to remove
   */
  public void removeQueryExecuteListener(QueryExecuteListener l) {
    m_QueryPanel.removeQueryExecuteListener(l);
  }

  /**
   * adds the given listener to the list of listeners
   * @param l       the listener to add to the list
   */
  public void addResultChangedListener(ResultChangedListener l) {
    m_ResultPanel.addResultChangedListener(l);
  }

  /**
   * removes the given listener from the list of listeners
   * @param l       the listener to remove
   */
  public void removeResultChangedListener(ResultChangedListener l) {
    m_ResultPanel.removeResultChangedListener(l);
  }

  /**
   * adds the given listener to the list of listeners
   * @param l       the listener to add to the list
   */
  public void addHistoryChangedListener(HistoryChangedListener l) {
    m_ConnectionPanel.addHistoryChangedListener(l);
    m_QueryPanel.addHistoryChangedListener(l);
  }

  /**
   * removes the given listener from the list of listeners
   * @param l       the listener to remove
   */
  public void removeHistoryChangedListener(HistoryChangedListener l) {
    m_ConnectionPanel.removeHistoryChangedListener(l);
    m_QueryPanel.removeHistoryChangedListener(l);
  }

  /** for monitoring the Memory consumption */
  private static Memory m_Memory = new Memory(true);
  
  /** the sql viewer */
  private static SqlViewer m_Viewer;
  
  /**
   * starts the SQL-Viewer interface.
   */
  public static void main(String[] args) {
    LookAndFeel.setLookAndFeel();
    
    try {
      // uncomment to disable the memory management:
      //m_Memory.setEnabled(false);

      final JFrame jf = new JFrame("Weka SQL-Viewer");
      m_Viewer = new SqlViewer(jf);
      jf.getContentPane().setLayout(new BorderLayout());
      jf.getContentPane().add(m_Viewer, BorderLayout.CENTER);
      jf.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          jf.dispose();
          System.exit(0);
        }
      });
      jf.pack();
      jf.setSize(800, 600);
      jf.setVisible(true);

      Thread memMonitor = new Thread() {
        public void run() {
          while (true) {
            try {
              this.sleep(4000);

              System.gc();

              if (m_Memory.isOutOfMemory()) {
                // clean up
                jf.dispose();
                m_Viewer = null;
                System.gc();

                // stop threads
                m_Memory.stopThreads();

                // display error
                System.err.println("\ndisplayed message:");
                m_Memory.showOutOfMemory();
                System.err.println("\nexiting");
                System.exit(-1);
              }

            } 
            catch (InterruptedException ex) { 
              ex.printStackTrace(); 
            }
          }
        }
      };

      memMonitor.setPriority(Thread.MAX_PRIORITY);
      memMonitor.start();
    } 
    catch (Exception ex) {
      ex.printStackTrace();
      System.err.println(ex.getMessage());
    }
  }
}

⌨️ 快捷键说明

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