📄 databasemanager.java
字号:
} catch (IOException e) {
return e.getMessage();
}
}
/**
* Method declaration
*
*
* @param file
* @param text
*/
void writeFile(String file, String text) {
try {
FileWriter write = new FileWriter(file);
write.write(text.toCharArray());
write.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Method declaration
*
*
* @param s
* @param help
*/
void showHelp(String s, String help) {
txtCommand.setText(s);
txtResult.setText(help);
bHelp = true;
pResult.removeAll();
pResult.add("Center", txtResult);
pResult.doLayout();
txtCommand.requestFocus();
txtCommand.setCaretPosition(s.length());
}
/**
* Method declaration
*
*
* @param e
*/
public void windowActivated(WindowEvent e) {}
/**
* Method declaration
*
*
* @param e
*/
public void windowDeactivated(WindowEvent e) {}
/**
* Method declaration
*
*
* @param e
*/
public void windowClosed(WindowEvent e) {}
/**
* Method declaration
*
*
* @param ev
*/
public void windowClosing(WindowEvent ev) {
try {
cConn.close();
} catch (Exception e) {}
fMain.dispose();
if (bMustExit) {
System.exit(0);
}
}
/**
* Method declaration
*
*
* @param e
*/
public void windowDeiconified(WindowEvent e) {}
/**
* Method declaration
*
*
* @param e
*/
public void windowIconified(WindowEvent e) {}
/**
* Method declaration
*
*
* @param e
*/
public void windowOpened(WindowEvent e) {}
/**
* Method declaration
*
*
* @param s
*/
void trace(String s) {
System.out.println(s);
}
/**
* Method declaration
*
*/
void execute() {
String sCmd = txtCommand.getText();
if (sCmd.startsWith("-->>>TEST<<<--")) {
testPerformance();
return;
}
String g[] = new String[1];
try {
lTime = System.currentTimeMillis();
sStatement.execute(sCmd);
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(txtCommand.getText());
} 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);
}
updateResult();
}
/**
* Method declaration
*
*/
void updateResult() {
if (iResult == 0) {
// in case 'help' has removed the grid
if (bHelp) {
pResult.removeAll();
pResult.add("Center", gResult);
pResult.doLayout();
bHelp = false;
}
gResult.update();
gResult.repaint();
} else {
showResultInText();
}
txtCommand.selectAll();
txtCommand.requestFocus();
}
/**
* Method declaration
*
*
* @param r
*/
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++) {
h[i - 1] = r.getString(i);
if (r.wasNull()) {
h[i - 1] = "(null)";
}
}
gResult.addRow(h);
}
r.close();
} catch (SQLException e) {}
}
/**
* Method declaration
*
*
* @param sql
* @param max
*
* @return
*
* @throws SQLException
*/
long testStatement(String sql, int max) throws SQLException {
long start = System.currentTimeMillis();
if (sql.indexOf('#') == -1) {
max = 1;
}
for (int i = 0; i < max; i++) {
String s = sql;
while (true) {
int j = s.indexOf("#r#");
if (j == -1) {
break;
}
s = s.substring(0, j) + ((int) (Math.random() * i))
+ s.substring(j + 3);
}
while (true) {
int j = s.indexOf('#');
if (j == -1) {
break;
}
s = s.substring(0, j) + i + s.substring(j + 1);
}
sStatement.execute(s);
}
return (System.currentTimeMillis() - start);
}
/**
* 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 = testStatement(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();
}
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();
}
/**
* Method declaration
*
*/
void showResultInText() {
String row[] = gResult.getHead();
int width = row.length;
Vector data = gResult.getData();
int size[] = new int[width];
for (int i = 0; i < width; i++) {
size[i] = row[i].length();
}
int len = data.size();
for (int i = 0; i < len; 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();
row = gResult.getHead();
for (int i = 0; i < width; i++) {
b.append(row[i]);
for (int l = row[i].length(); l <= size[i]; l++) {
b.append(' ');
}
}
b.append('\n');
for (int i = 0; i < width; i++) {
for (int l = 0; l < size[i]; l++) {
b.append('-');
}
b.append(' ');
}
b.append('\n');
for (int i = 0; i < len; 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('\n');
}
b.append("\n" + len + " 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");
butExecute.addActionListener(this);
pCommand.add("East", butExecute);
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();
tTree.setMinimumSize(new Dimension(200, 100));
gResult.setMinimumSize(new Dimension(200, 300));
fMain.add("West", tTree);
doLayout();
fMain.pack();
}
/**
* Method declaration
*
*/
private 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"
};
ResultSet result = dMeta.getTables(null, null, null, usertables);
Vector tables = new Vector();
while (result.next()) {
tables.addElement(result.getString(3));
}
result.close();
for (int i = 0; i < tables.size(); i++) {
String name = (String) tables.elementAt(i);
String key = "tab-" + name + "-";
tTree.addRow(key, name, "+", color_table);
ResultSet col = dMeta.getColumns(null, null, name, null);
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);
}
col.close();
tTree.addRow(key + "ind", "Indices", "+", 0);
ResultSet ind = dMeta.getIndexInfo(null, null, name, false,
false);
String oldiname = null;
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);
}
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 + -