📄 databasemanagerswing.java
字号:
// restore the cursors we saved fMain.setCursor(fMainCursor); txtCommand.setCursor(txtCommandCursor); txtResult.setCursor(txtResultCursor); //TODO: Enable actionButtons } else { // save the old cursors if (fMainCursor == null) { fMainCursor = fMain.getCursor(); txtCommandCursor = txtCommand.getCursor(); txtResultCursor = txtResult.getCursor(); } // set the cursors to the wait cursor fMain.setCursor(waitCursor); txtCommand.setCursor(waitCursor); txtResult.setCursor(waitCursor); //TODO: Disable actionButtons } setStatusLine(busyText); } 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() { backgroundIt(new StatementExecRunnable(), "Executing SQL"); } protected class StatementExecRunnable implements Runnable { private String sCmd; protected StatementExecRunnable() { if (4096 <= ifHuge.length()) { sCmd = ifHuge; } else { sCmd = txtCommand.getText(); } } public void run() { gResult.clear(); try { if (sCmd.startsWith("-->>>TEST<<<--")) { testPerformance(); } else { executeSQL(); } updateResult(); if (gridFormat) { gResult.fireTableChanged(null); } updateAutoCommitBox(); System.gc(); } catch (RuntimeException re) { CommonSwing.errorMessage(re); throw re; } finally { setWaiting(null); } } } ; private void executeSQL() { String[] g = new String[1]; String sql = txtCommand.getText(); try { lTime = System.currentTimeMillis(); 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; addToRecent(sql); } 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"); 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(); } } } 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 where 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) { g[0] = g[1] = "n/a"; g[3] = e.toString(); // Added: (weconsultants@users) CommonSwing.errorMessage(e); } gResult.addRow(g); System.out.println(l + " ms : " + sql); } g[0] = "" + total; g[1] = "total"; g[2] = ""; gResult.addRow(g); lTime = System.currentTimeMillis() - lTime; } /** * Method declaration * */ private void showResultInText() { Object[] col = gResult.getHead(); int width = col.length; int[] size = new int[width]; Vector data = gResult.getData(); Object[] row; int height = data.size(); for (int i = 0; i < width; i++) { size[i] = col[i].toString().length(); } for (int i = 0; i < height; i++) { row = (Object[]) data.elementAt(i); for (int j = 0; j < width; j++) { String item = ((row[j] == null) ? "" : row[j].toString()); int l = item.length(); if (l > size[j]) { size[j] = l; } } } StringBuffer b = new StringBuffer(); for (int i = 0; i < width; i++) { b.append(col[i]); for (int l = col[i].toString().length(); l <= size[i]; l++) { b.append(' '); } } b.append(NL); for (int i = 0; i < width; i++) { for (int l = 0; l < size[i]; l++) { b.append('-'); } b.append(' '); } b.append(NL); for (int i = 0; i < height; i++) { row = (Object[]) data.elementAt(i); for (int j = 0; j < width; j++) { String item = ((row[j] == null) ? "" : row[j].toString()); b.append(item); for (int l = item.length(); l <= size[j]; l++) { b.append(' '); } } b.append(NL); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -