@@ -21,13 +21,13 @@ public class BufferStatement {
21
21
* The values to replace <bold>?</bold> with in
22
22
* <bold>query</bold>. These are in order.
23
23
*/
24
+ private static final Exception sharedException = new Exception ();
25
+
24
26
public BufferStatement (String query , Object ... values ) {
25
27
this .query = query ;
26
28
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 ();
31
31
}
32
32
33
33
/**
@@ -44,8 +44,9 @@ public BufferStatement(String query, Object... values) {
44
44
*/
45
45
public PreparedStatement prepareStatement (Connection con ) throws SQLException {
46
46
PreparedStatement ps ;
47
+ int valuesLength = values .length ; // 缓存数组长度
47
48
ps = con .prepareStatement (query );
48
- for (int i = 0 ; i < values . length ; i ++) {
49
+ for (int i = 0 ; i < valuesLength ; i ++) {
49
50
ps .setObject (i + 1 , values [i ]);
50
51
}
51
52
return ps ;
@@ -70,6 +71,8 @@ public StackTraceElement[] getStackTrace() {
70
71
*/
71
72
@ Override
72
73
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 ();
74
77
}
75
78
}
0 commit comments