Skip to content

Commit eb8dd31

Browse files
committed
优化异常处理,缓存数组长度,重写toString方法以提高性能
1 parent 8228ed7 commit eb8dd31

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/main/java/cc/baka9/catseedlogin/bukkit/database/BufferStatement.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ public class BufferStatement {
2121
* The values to replace <bold>?</bold> with in
2222
* <bold>query</bold>. These are in order.
2323
*/
24+
private static final Exception sharedException = new Exception();
25+
2426
public BufferStatement(String query, Object... values) {
2527
this.query = query;
2628
this.values = values;
27-
// For error handling
28-
this.stacktrace = new Exception();
29-
this.stacktrace.fillInStackTrace(); // We can declare where this
30-
// statement came from.
29+
this.stacktrace = sharedException; // 重复利用一个已存在的 Exception 对象
30+
this.stacktrace.fillInStackTrace();
3131
}
3232

3333
/**
@@ -44,8 +44,9 @@ public BufferStatement(String query, Object... values) {
4444
*/
4545
public PreparedStatement prepareStatement(Connection con) throws SQLException {
4646
PreparedStatement ps;
47+
int valuesLength = values.length; // 缓存数组长度
4748
ps = con.prepareStatement(query);
48-
for (int i = 0; i < values.length; i++) {
49+
for (int i = 0; i < valuesLength; i++) {
4950
ps.setObject(i + 1, values[i]);
5051
}
5152
return ps;
@@ -70,6 +71,8 @@ public StackTraceElement[] getStackTrace() {
7071
*/
7172
@Override
7273
public String toString() {
73-
return "Query: " + query + ", values: " + Arrays.toString(values);
74+
StringBuilder sb = new StringBuilder();
75+
sb.append("Query: ").append(query).append(", values: ").append(Arrays.toString(values));
76+
return sb.toString();
7477
}
7578
}

0 commit comments

Comments
 (0)