📄 databasemanager.java
字号:
*/ 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(); String[] h = new String[col]; for (int i = 1; i <= col; i++) { h[i - 1] = m.getColumnLabel(i); } gResult.setHead(h); while (r.next()) { for (int i = 1; i <= col; i++) { try { h[i - 1] = r.getString(i); if (r.wasNull()) { h[i - 1] = "(null)"; } } catch (SQLException e) {} } gResult.addRow(h); } r.close(); } catch (SQLException e) {} } /** * Method declaration * */ 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] = String.valueOf(l); g[1] = String.valueOf(max); g[3] = ""; } catch (SQLException e) { g[0] = g[1] = "n/a"; g[3] = e.toString(); } gResult.addRow(g); System.out.println(l + " ms : " + sql); } g[0] = "" + total; g[1] = "total"; g[2] = ""; gResult.addRow(g); lTime = System.currentTimeMillis() - lTime; updateResult(); } void saveAsCsv(String filename) { try { File file = new File(filename); CSVWriter writer = new CSVWriter(file, null); String[] col = gResult.getHead(); int width = col.length; Vector data = gResult.getData(); String[] row; int height = data.size(); writer.writeHeader(col); for (int i = 0; i < height; i++) { row = (String[]) data.elementAt(i); String[] myRow = new String[row.length]; for (int j = 0; j < row.length; j++) { String r = row[j]; if (r.equals("(null)")) { // null is formatted as (null) r = ""; } myRow[j] = r; } writer.writeData(myRow); } writer.close(); } catch (IOException e) { throw new RuntimeException("IOError: " + e.getMessage()); } } /** * Method declaration * */ void showResultInText() { String[] col = gResult.getHead(); int width = col.length; int[] size = new int[width]; Vector data = gResult.getData(); String[] row; int height = data.size(); for (int i = 0; i < width; i++) { size[i] = col[i].length(); } for (int i = 0; i < height; i++) { row = (String[]) data.elementAt(i); for (int j = 0; j < width; j++) { int l = row[j].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].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 = (String[]) data.elementAt(i); for (int j = 0; j < width; j++) { b.append(row[j]); for (int l = row[j].length(); l <= size[j]; l++) { b.append(' '); } } b.append(NL); } b.append(NL + height + " row(s) in " + lTime + " ms"); txtResult.setText(b.toString()); } /** * Method declaration * * * @param s */ private void addToRecent(String s) { for (int i = 0; i < iMaxRecent; i++) { if (s.equals(sRecent[i])) { return; } } if (sRecent[iRecent] != null) { mRecent.remove(iRecent); } sRecent[iRecent] = s; if (s.length() > 43) { s = s.substring(0, 40) + "..."; } MenuItem item = new MenuItem(s); item.setActionCommand("#" + iRecent); item.addActionListener(this); mRecent.insert(item, iRecent); iRecent = (iRecent + 1) % iMaxRecent; } /** * Method declaration * */ private void initGUI() { Panel pQuery = new Panel(); Panel pCommand = new Panel(); pResult = new Panel(); pQuery.setLayout(new BorderLayout()); pCommand.setLayout(new BorderLayout()); pResult.setLayout(new BorderLayout()); Font fFont = new Font("Dialog", Font.PLAIN, 12); txtCommand = new TextArea(5, 40); txtCommand.addKeyListener(this); txtResult = new TextArea(20, 40); txtCommand.setFont(fFont); txtResult.setFont(new Font("Courier", Font.PLAIN, 12)); butExecute = new Button("Execute"); butClear = new Button("Clear"); butExecute.addActionListener(this); butClear.addActionListener(this); pCommand.add("East", butExecute); pCommand.add("West", butClear); pCommand.add("Center", txtCommand); gResult = new Grid(); setLayout(new BorderLayout()); pResult.add("Center", gResult); pQuery.add("North", pCommand); pQuery.add("Center", pResult); fMain.add("Center", pQuery); tTree = new Tree(); // (ulrivo): screen with less than 640 width Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); if (d.width >= 640) { tTree.setMinimumSize(new Dimension(200, 100)); } else { tTree.setMinimumSize(new Dimension(80, 100)); } gResult.setMinimumSize(new Dimension(200, 300)); fMain.add("West", tTree); doLayout(); fMain.pack(); } /** * Method declaration * */ protected void refreshTree() { tTree.removeAll(); try { int color_table = Color.yellow.getRGB(); int color_column = Color.orange.getRGB(); int color_index = Color.red.getRGB(); tTree.addRow("", dMeta.getURL(), "-", 0); String[] usertables = { "TABLE", "GLOBAL TEMPORARY", "VIEW" }; // fredt@users Schema support Vector schemas = new Vector(); Vector tables = new Vector(); // sqlbob@users Added remarks. Vector remarks = new Vector(); ResultSet result = dMeta.getTables(null, null, null, usertables); try { while (result.next()) { schemas.addElement(result.getString(2)); tables.addElement(result.getString(3)); remarks.addElement(result.getString(5)); } } finally { result.close(); } for (int i = 0; i < tables.size(); i++) { String name = (String) tables.elementAt(i); String schema = (String) schemas.elementAt(i); String key = "tab-" + name + "-"; tTree.addRow(key, name, "+", color_table); // sqlbob@users Added remarks. String remark = (String) remarks.elementAt(i); if ((schema != null) &&!schema.trim().equals("")) { tTree.addRow(key + "s", "schema: " + schema); } if ((remark != null) &&!remark.trim().equals("")) { tTree.addRow(key + "r", " " + remark); } ResultSet col = dMeta.getColumns(null, schema, name, null); try { while (col.next()) { String c = col.getString(4); String k1 = key + "col-" + c + "-"; tTree.addRow(k1, c, "+", color_column); String type = col.getString(6); tTree.addRow(k1 + "t", "Type: " + type); boolean nullable = col.getInt(11) != DatabaseMetaData.columnNoNulls; tTree.addRow(k1 + "n", "Nullable: " + nullable); } } finally { col.close(); } tTree.addRow(key + "ind", "Indices", "+", 0); ResultSet ind = dMeta.getIndexInfo(null, schema, name, false, false); String oldiname = null; try { while (ind.next()) { boolean nonunique = ind.getBoolean(4); String iname = ind.getString(6); String k2 = key + "ind-" + iname + "-"; if ((oldiname == null ||!oldiname.equals(iname))) { tTree.addRow(k2, iname, "+", color_index); tTree.addRow(k2 + "u", "Unique: " + !nonunique); oldiname = iname; } String c = ind.getString(9); tTree.addRow(k2 + "c-" + c + "-", c); } } finally { ind.close(); } } tTree.addRow("p", "Properties", "+", 0); tTree.addRow("pu", "User: " + dMeta.getUserName()); tTree.addRow("pr", "ReadOnly: " + cConn.isReadOnly()); tTree.addRow("pa", "AutoCommit: " + cConn.getAutoCommit()); tTree.addRow("pd", "Driver: " + dMeta.getDriverName()); tTree.addRow("pp", "Product: " + dMeta.getDatabaseProductName()); tTree.addRow("pv", "Version: " + dMeta.getDatabaseProductVersion()); } catch (SQLException e) { tTree.addRow("", "Error getting metadata:", "-", 0); tTree.addRow("-", e.getMessage()); tTree.addRow("-", e.getSQLState()); } tTree.update(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -