📄 sqlviewer.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* SqlViewer.java
* Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
*
*/
package weka.gui.sql;
import weka.core.Memory;
import weka.core.Utils;
import weka.gui.LookAndFeel;
import weka.gui.sql.event.ConnectionEvent;
import weka.gui.sql.event.ConnectionListener;
import weka.gui.sql.event.HistoryChangedEvent;
import weka.gui.sql.event.HistoryChangedListener;
import weka.gui.sql.event.QueryExecuteEvent;
import weka.gui.sql.event.QueryExecuteListener;
import weka.gui.sql.event.ResultChangedEvent;
import weka.gui.sql.event.ResultChangedListener;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* Represents a little tool for querying SQL databases.
*
*
* @author FracPete (fracpete at waikato dot ac dot nz)
* @version $Revision: 1.3 $
*/
public class SqlViewer
extends JPanel
implements ConnectionListener,
HistoryChangedListener,
QueryExecuteListener,
ResultChangedListener {
/** the name of the history file (in the home directory) */
protected final static String HISTORY_FILE = "SqlViewerHistory.props";
/** the parent of this panel */
protected JFrame m_Parent;
/** the connection panel */
protected ConnectionPanel m_ConnectionPanel;
/** the query panel */
protected QueryPanel m_QueryPanel;
/** the result panel */
protected ResultPanel m_ResultPanel;
/** the info panel */
protected InfoPanel m_InfoPanel;
/** the connect string with which the query was run */
protected String m_URL;
/** the user that was used to connect to the DB */
protected String m_User;
/** the password that was used to connect to the DB */
protected String m_Password;
/** the currently selected query */
protected String m_Query;
/** stores the history */
protected Properties m_History;
/**
* initializes the SqlViewer
* @param parent the parent of this panel
*/
public SqlViewer(JFrame parent) {
super();
m_Parent = parent;
m_URL = "";
m_User = "";
m_Password = "";
m_Query = "";
m_History = new Properties();
createPanel();
}
/**
* builds the interface
*/
protected void createPanel() {
JPanel panel;
JPanel panel2;
setLayout(new BorderLayout());
// connection
m_ConnectionPanel = new ConnectionPanel(m_Parent);
panel = new JPanel(new BorderLayout());
add(panel, BorderLayout.NORTH);
panel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Connection"),
BorderFactory.createEmptyBorder(0, 5, 5, 5)));
panel.add(m_ConnectionPanel, BorderLayout.CENTER);
// query
m_QueryPanel = new QueryPanel(m_Parent);
panel = new JPanel(new BorderLayout());
add(panel, BorderLayout.CENTER);
panel2 = new JPanel(new BorderLayout());
panel2.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Query"),
BorderFactory.createEmptyBorder(0, 5, 5, 5)));
panel2.add(m_QueryPanel, BorderLayout.NORTH);
panel.add(panel2, BorderLayout.NORTH);
// result
m_ResultPanel = new ResultPanel(m_Parent);
m_ResultPanel.setQueryPanel(m_QueryPanel);
panel2 = new JPanel(new BorderLayout());
panel2.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Result"),
BorderFactory.createEmptyBorder(0, 5, 5, 5)));
panel2.add(m_ResultPanel, BorderLayout.CENTER);
panel.add(panel2, BorderLayout.CENTER);
// info
m_InfoPanel = new InfoPanel(m_Parent);
panel = new JPanel(new BorderLayout());
add(panel, BorderLayout.SOUTH);
panel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Info"),
BorderFactory.createEmptyBorder(0, 5, 5, 5)));
panel.add(m_InfoPanel, BorderLayout.CENTER);
// listeners
addConnectionListener(this);
addConnectionListener(m_QueryPanel);
addQueryExecuteListener(this);
addQueryExecuteListener(m_ResultPanel);
addResultChangedListener(this);
addHistoryChangedListener(this);
// history
loadHistory(true);
}
/**
* This method gets called when the connection is either established
* or disconnected.
*/
public void connectionChange(ConnectionEvent evt) {
if (evt.getType() == ConnectionEvent.DISCONNECT) {
m_InfoPanel.append( "disconnect from: "
+ evt.getDbUtils().getDatabaseURL(),
"information_small.gif" );
}
else {
m_InfoPanel.append( "connecting to: "
+ evt.getDbUtils().getDatabaseURL()
+ " = " + evt.isConnected(),
"information_small.gif" );
}
// did an exception happen?
if (evt.getException() != null)
m_InfoPanel.append("exception: " + evt.getException(), "error_small.gif");
// set focus
if (evt.isConnected())
m_QueryPanel.setFocus();
else
m_ConnectionPanel.setFocus();
}
/**
* This method gets called when a query has been executed.
*/
public void queryExecuted(QueryExecuteEvent evt) {
ResultSetHelper helper;
if (evt.failed()) {
m_InfoPanel.append("Query:" + evt.getQuery(), "error_small.gif");
m_InfoPanel.append("exception: " + evt.getException(), "error_small.gif");
}
else {
m_InfoPanel.append("Query: " + evt.getQuery(), "information_small.gif");
try {
if (evt.hasResult()) {
helper = new ResultSetHelper(evt.getResultSet());
if (evt.getMaxRows() > 0)
m_InfoPanel.append(helper.getRowCount() + " rows selected ("
+ evt.getMaxRows() + " displayed).",
"information_small.gif");
else
m_InfoPanel.append(helper.getRowCount() + " rows selected.",
"information_small.gif");
}
// save max rows
loadHistory(false);
m_History.setProperty(
QueryPanel.MAX_ROWS, Integer.toString(evt.getMaxRows()));
saveHistory();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* This method gets called when a query has been executed.
*/
public void resultChanged(ResultChangedEvent evt) {
m_URL = evt.getURL();
m_User = evt.getUser();
m_Password = evt.getPassword();
m_Query = evt.getQuery();
}
/**
* This method gets called when a history is modified.
* It saves the history immediately to the users home directory.
*/
public void historyChanged(HistoryChangedEvent evt) {
// load history, in case some other process changed it!
loadHistory(false);
m_History.setProperty(
evt.getHistoryName(), modelToString(evt.getHistory()));
// save it
saveHistory();
}
/**
* returns the filename of the history file
*/
protected String getHistoryFilename() {
return System.getProperties().getProperty("user.home")
+ File.separatorChar
+ HISTORY_FILE;
}
/**
* transforms the given, comma-separated string into a DefaultListModel
* @param s the string to break up and transform into a list model
* @return the generated DefaultListModel
*/
protected DefaultListModel stringToModel(String s) {
DefaultListModel result;
String tmpStr;
int i;
boolean quote;
String[] find;
String[] replace;
int index;
result = new DefaultListModel();
// get rid of doubled quotes, \\n, etc.
find = new String[]{"\"\"", "\\n", "\\r", "\\t"};
replace = new String[]{"\"", "\n", "\r", "\t"};
for (i = 0; i < find.length; i++) {
tmpStr = "";
while (s.length() > 0) {
index = s.indexOf(find[i]);
if (index > -1) {
tmpStr += s.substring(0, index) + replace[i];
s = s.substring(index + 2);
}
else {
tmpStr += s;
s = "";
}
}
s = tmpStr;
}
quote = false;
tmpStr = "";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -