Skip to content

Commit

Permalink
优化异常处理,缓存数组长度,重写toString方法以提高性能
Browse files Browse the repository at this point in the history
  • Loading branch information
shulng committed May 28, 2024
1 parent 8228ed7 commit eb8dd31
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public class BufferStatement {
* The values to replace <bold>?</bold> with in
* <bold>query</bold>. These are in order.
*/
private static final Exception sharedException = new Exception();

public BufferStatement(String query, Object... values) {
this.query = query;
this.values = values;
// For error handling
this.stacktrace = new Exception();
this.stacktrace.fillInStackTrace(); // We can declare where this
// statement came from.
this.stacktrace = sharedException; // 重复利用一个已存在的 Exception 对象
this.stacktrace.fillInStackTrace();
}

/**
Expand All @@ -44,8 +44,9 @@ public BufferStatement(String query, Object... values) {
*/
public PreparedStatement prepareStatement(Connection con) throws SQLException {
PreparedStatement ps;
int valuesLength = values.length; // 缓存数组长度
ps = con.prepareStatement(query);
for (int i = 0; i < values.length; i++) {
for (int i = 0; i < valuesLength; i++) {
ps.setObject(i + 1, values[i]);
}
return ps;
Expand All @@ -70,6 +71,8 @@ public StackTraceElement[] getStackTrace() {
*/
@Override
public String toString() {
return "Query: " + query + ", values: " + Arrays.toString(values);
StringBuilder sb = new StringBuilder();
sb.append("Query: ").append(query).append(", values: ").append(Arrays.toString(values));
return sb.toString();
}
}

0 comments on commit eb8dd31

Please sign in to comment.