📄 databasemanagerswing.java
字号:
txtResult.setCursor(txtResultCursor);
//TODO: Enable actionButtons
} else {
// save the old cursors
if (fMainCursor == null) {
fMainCursor = ((fMain instanceof java.awt.Frame)
? (((java.awt.Frame) fMain).getCursor())
: (((Component) fMain).getCursor()));
txtCommandCursor = txtCommand.getCursor();
txtResultCursor = txtResult.getCursor();
}
// set the cursors to the wait cursor
if (fMain instanceof java.awt.Frame) {
((java.awt.Frame) fMain).setCursor(waitCursor);
} else {
((Component) fMain).setCursor(waitCursor);
}
txtCommand.setCursor(waitCursor);
txtResult.setCursor(waitCursor);
//TODO: Disable actionButtons
}
setStatusLine(busyText, ((busyText == null) ? gResult.getRowCount()
: 0));
}
private Runnable enableButtonRunnable = new Runnable() {
public void run() {
jbuttonClear.setEnabled(true);
jbuttonExecute.setEnabled(true);
}
};
private Runnable disableButtonRunnable = new Runnable() {
public void run() {
jbuttonClear.setEnabled(false);
jbuttonExecute.setEnabled(false);
}
};
private Thread buttonUpdaterThread = null;
private static final int BUTTON_CHECK_PERIOD = 500;
private Runnable buttonUpdater = new Runnable() {
public void run() {
boolean havesql;
while (true) {
try {
Thread.sleep(BUTTON_CHECK_PERIOD);
} catch (InterruptedException ie) {}
if (buttonUpdaterThread == null) { // Pointer to me
return;
}
havesql = (txtCommand.getText().length() > 0);
if (jbuttonClear.isEnabled() != havesql) {
SwingUtilities.invokeLater(havesql ? enableButtonRunnable
: disableButtonRunnable);
}
}
}
};
private JButton jbuttonClear;
private JButton jbuttonExecute;
public void start() {
if (buttonUpdaterThread == null) {
buttonUpdaterThread = new Thread(buttonUpdater);
}
buttonUpdaterThread.start();
}
public void stop() {
System.err.println("Stopping");
buttonUpdaterThread = null;
}
private Runnable treeRefreshRunnable = new Runnable() {
public void run() {
try {
directRefreshTree();
} catch (RuntimeException re) {
CommonSwing.errorMessage(re);
throw re;
} finally {
setWaiting(null);
}
}
};
/**
* Schedules to run in a Gui-safe thread
*/
protected void executeCurrentSQL() {
if (txtCommand.getText().length() < 1) {
CommonSwing.errorMessage("No SQL to execute");
return;
}
backgroundIt(new StatementExecRunnable(), "Executing SQL");
}
protected class StatementExecRunnable implements Runnable {
public void run() {
gResult.clear();
try {
if (txtCommand.getText().startsWith("-->>>TEST<<<--")) {
testPerformance();
} else {
executeSQL();
}
updateResult();
displayResults();
updateAutoCommitBox();
System.gc();
} catch (RuntimeException re) {
CommonSwing.errorMessage(re);
throw re;
} finally {
setWaiting(null);
}
}
}
;
private void executeSQL() {
String[] g = new String[1];
String sql = null;
try {
lTime = System.currentTimeMillis();
sql = ((sqlScriptBuffer == null ? txtCommand.getText()
: sqlScriptBuffer));
sStatement.execute(sql);
int r = sStatement.getUpdateCount();
if (r == -1) {
formatResultSet(sStatement.getResultSet());
} else {
g[0] = "update count";
gResult.setHead(g);
g[0] = "" + r;
gResult.addRow(g);
}
lTime = System.currentTimeMillis() - lTime;
if (sqlScriptBuffer == null) {
addToRecent(sql);
txtCommand.setEnabled(true); // clear() does this otherwise
} else {
clear();
}
} catch (SQLException e) {
lTime = System.currentTimeMillis() - lTime;
g[0] = "SQL Error";
gResult.setHead(g);
String s = e.getMessage();
s += " / Error Code: " + e.getErrorCode();
s += " / State: " + e.getSQLState();
g[0] = s;
gResult.addRow(g);
// Added: (weconsultants@users)
CommonSwing.errorMessage(e);
return;
}
if (autoRefresh) {
// We're already running in a "busy" thread. Just update the
// status text.
setStatusLine("Refreshing object tree", 0);
String upper = sql.toUpperCase(Locale.ENGLISH);
// This test can be very liberal. Too liberal will just do
// some extra refreshes. Too conservative will display
// obsolete info.
if (upper.indexOf("ALTER") > -1 || upper.indexOf("DROP") > -1
|| upper.indexOf("CREATE") > -1) {
directRefreshTree();
}
}
}
/**
* Could somebody explain what the purpose of this method is?
* Contrary to the method name, it looks like it displays
* results only if gridFormat is off (seems like it does
* nothing otherwise, except for clearing help text and moving focus).
*/
private void updateResult() {
if (gridFormat) {
// in case 'help' has removed the grid
if (bHelp) {
pResult.removeAll();
pResult.add(gScrollPane, BorderLayout.CENTER);
pResult.doLayout();
gResult.fireTableChanged(null);
pResult.repaint();
bHelp = false;
}
} else {
showResultInText();
}
txtCommand.selectAll();
txtCommand.requestFocus();
}
/**
* We let Swing handle displaying nulls (which it generally does by
* printing nothing for them), except for the case of database
* VARCHARs, because this is the only class where there is any
* ambiguity about whether there is a null stored or not.
*/
private void formatResultSet(ResultSet r) {
if (r == null) {
String[] g = new String[1];
g[0] = "Result";
gResult.setHead(g);
g[0] = "(empty)";
gResult.addRow(g);
return;
}
try {
ResultSetMetaData m = r.getMetaData();
int col = m.getColumnCount();
Object[] h = new Object[col];
boolean[] isVarChar = new boolean[col];
for (int i = 1; i <= col; i++) {
h[i - 1] = m.getColumnLabel(i);
isVarChar[i - 1] = (m.getColumnType(i)
== java.sql.Types.VARCHAR);
}
gResult.setHead(h);
while (r.next()) {
for (int i = 1; i <= col; i++) {
try {
h[i - 1] = r.getObject(i);
if (r.wasNull()) {
h[i - 1] = (isVarChar[i - 1] ? NULL_STR
: null);
}
} catch (SQLException e) {}
}
gResult.addRow(h);
}
r.close();
} catch (SQLException e) {
// Added: (weconsultants@users)
CommonSwing.errorMessage(e);
}
}
private void testPerformance() {
String all = txtCommand.getText();
StringBuffer b = new StringBuffer();
long total = 0;
for (int i = 0; i < all.length(); i++) {
char c = all.charAt(i);
if (c != '\n') {
b.append(c);
}
}
all = b.toString();
String[] g = new String[4];
g[0] = "ms";
g[1] = "count";
g[2] = "sql";
g[3] = "error";
gResult.setHead(g);
int max = 1;
lTime = System.currentTimeMillis() - lTime;
while (!all.equals("")) {
int i = all.indexOf(';');
String sql;
if (i != -1) {
sql = all.substring(0, i);
all = all.substring(i + 1);
} else {
sql = all;
all = "";
}
if (sql.startsWith("--#")) {
max = Integer.parseInt(sql.substring(3));
continue;
} else if (sql.startsWith("--")) {
continue;
}
g[2] = sql;
long l = 0;
try {
l = DatabaseManagerCommon.testStatement(sStatement, sql, max);
total += l;
g[0] = "" + l;
g[1] = "" + max;
g[3] = "";
} catch (SQLException e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -