1
1
/**
2
- * Copyright 2009-2019 the original author or authors.
2
+ * Copyright 2009-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -77,17 +77,10 @@ public Map<String, Object> selectOne(String sql, Object... args) throws SQLExcep
77
77
* @throws SQLException If statement preparation or execution fails
78
78
*/
79
79
public List <Map <String , Object >> selectAll (String sql , Object ... args ) throws SQLException {
80
- PreparedStatement ps = connection .prepareStatement (sql );
81
- try {
80
+ try (PreparedStatement ps = connection .prepareStatement (sql );) {
82
81
setParameters (ps , args );
83
82
ResultSet rs = ps .executeQuery ();
84
83
return getResults (rs );
85
- } finally {
86
- try {
87
- ps .close ();
88
- } catch (SQLException e ) {
89
- //ignore
90
- }
91
84
}
92
85
}
93
86
@@ -146,16 +139,9 @@ public int insert(String sql, Object... args) throws SQLException {
146
139
* @throws SQLException If statement preparation or execution fails
147
140
*/
148
141
public int update (String sql , Object ... args ) throws SQLException {
149
- PreparedStatement ps = connection .prepareStatement (sql );
150
- try {
142
+ try (PreparedStatement ps = connection .prepareStatement (sql );) {
151
143
setParameters (ps , args );
152
144
return ps .executeUpdate ();
153
- } finally {
154
- try {
155
- ps .close ();
156
- } catch (SQLException e ) {
157
- //ignore
158
- }
159
145
}
160
146
}
161
147
@@ -179,15 +165,8 @@ public int delete(String sql, Object... args) throws SQLException {
179
165
* @throws SQLException If statement preparation or execution fails
180
166
*/
181
167
public void run (String sql ) throws SQLException {
182
- Statement stmt = connection .createStatement ();
183
- try {
168
+ try (Statement stmt = connection .createStatement ();) {
184
169
stmt .execute (sql );
185
- } finally {
186
- try {
187
- stmt .close ();
188
- } catch (SQLException e ) {
189
- //ignore
190
- }
191
170
}
192
171
}
193
172
@@ -220,44 +199,37 @@ private void setParameters(PreparedStatement ps, Object... args) throws SQLExcep
220
199
}
221
200
}
222
201
202
+ /**
203
+ * ResultSet should't was closed in here,it should be closed in caller
204
+ */
223
205
private List <Map <String , Object >> getResults (ResultSet rs ) throws SQLException {
224
- try {
225
- List <Map <String , Object >> list = new ArrayList <>();
226
- List <String > columns = new ArrayList <>();
227
- List <TypeHandler <?>> typeHandlers = new ArrayList <>();
228
- ResultSetMetaData rsmd = rs .getMetaData ();
229
- for (int i = 0 , n = rsmd .getColumnCount (); i < n ; i ++) {
230
- columns .add (rsmd .getColumnLabel (i + 1 ));
231
- try {
232
- Class <?> type = Resources .classForName (rsmd .getColumnClassName (i + 1 ));
233
- TypeHandler <?> typeHandler = typeHandlerRegistry .getTypeHandler (type );
234
- if (typeHandler == null ) {
235
- typeHandler = typeHandlerRegistry .getTypeHandler (Object .class );
236
- }
237
- typeHandlers .add (typeHandler );
238
- } catch (Exception e ) {
239
- typeHandlers .add (typeHandlerRegistry .getTypeHandler (Object .class ));
240
- }
241
- }
242
- while (rs .next ()) {
243
- Map <String , Object > row = new HashMap <>();
244
- for (int i = 0 , n = columns .size (); i < n ; i ++) {
245
- String name = columns .get (i );
246
- TypeHandler <?> handler = typeHandlers .get (i );
247
- row .put (name .toUpperCase (Locale .ENGLISH ), handler .getResult (rs , name ));
206
+ List <Map <String , Object >> list = new ArrayList <>();
207
+ List <String > columns = new ArrayList <>();
208
+ List <TypeHandler <?>> typeHandlers = new ArrayList <>();
209
+ ResultSetMetaData rsmd = rs .getMetaData ();
210
+ for (int i = 0 , n = rsmd .getColumnCount (); i < n ; i ++) {
211
+ columns .add (rsmd .getColumnLabel (i + 1 ));
212
+ try {
213
+ Class <?> type = Resources .classForName (rsmd .getColumnClassName (i + 1 ));
214
+ TypeHandler <?> typeHandler = typeHandlerRegistry .getTypeHandler (type );
215
+ if (typeHandler == null ) {
216
+ typeHandler = typeHandlerRegistry .getTypeHandler (Object .class );
248
217
}
249
- list .add (row );
218
+ typeHandlers .add (typeHandler );
219
+ } catch (Exception e ) {
220
+ typeHandlers .add (typeHandlerRegistry .getTypeHandler (Object .class ));
250
221
}
251
- return list ;
252
- } finally {
253
- if (rs != null ) {
254
- try {
255
- rs .close ();
256
- } catch (Exception e ) {
257
- // ignore
258
- }
222
+ }
223
+ while (rs .next ()) {
224
+ Map <String , Object > row = new HashMap <>();
225
+ for (int i = 0 , n = columns .size (); i < n ; i ++) {
226
+ String name = columns .get (i );
227
+ TypeHandler <?> handler = typeHandlers .get (i );
228
+ row .put (name .toUpperCase (Locale .ENGLISH ), handler .getResult (rs , name ));
259
229
}
230
+ list .add (row );
260
231
}
232
+ return list ;
261
233
}
262
234
263
235
}
0 commit comments