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,16 +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
- ResultSet rs = ps .executeQuery ();
84
- return getResults (rs );
85
- } finally {
86
- try {
87
- ps .close ();
88
- } catch (SQLException e ) {
89
- //ignore
82
+ try (ResultSet rs = ps .executeQuery ()) {
83
+ return getResults (rs );
90
84
}
91
85
}
92
86
}
@@ -111,17 +105,19 @@ public int insert(String sql, Object... args) throws SQLException {
111
105
setParameters (ps , args );
112
106
ps .executeUpdate ();
113
107
if (useGeneratedKeySupport ) {
114
- List <Map <String , Object >> keys = getResults (ps .getGeneratedKeys ());
115
- if (keys .size () == 1 ) {
116
- Map <String , Object > key = keys .get (0 );
117
- Iterator <Object > i = key .values ().iterator ();
118
- if (i .hasNext ()) {
119
- Object genkey = i .next ();
120
- if (genkey != null ) {
121
- try {
122
- return Integer .parseInt (genkey .toString ());
123
- } catch (NumberFormatException e ) {
124
- //ignore, no numeric key support
108
+ try (ResultSet generatedKeys = ps .getGeneratedKeys ()) {
109
+ List <Map <String , Object >> keys = getResults (generatedKeys );
110
+ if (keys .size () == 1 ) {
111
+ Map <String , Object > key = keys .get (0 );
112
+ Iterator <Object > i = key .values ().iterator ();
113
+ if (i .hasNext ()) {
114
+ Object genkey = i .next ();
115
+ if (genkey != null ) {
116
+ try {
117
+ return Integer .parseInt (genkey .toString ());
118
+ } catch (NumberFormatException e ) {
119
+ //ignore, no numeric key support
120
+ }
125
121
}
126
122
}
127
123
}
@@ -146,16 +142,9 @@ public int insert(String sql, Object... args) throws SQLException {
146
142
* @throws SQLException If statement preparation or execution fails
147
143
*/
148
144
public int update (String sql , Object ... args ) throws SQLException {
149
- PreparedStatement ps = connection .prepareStatement (sql );
150
- try {
145
+ try (PreparedStatement ps = connection .prepareStatement (sql )) {
151
146
setParameters (ps , args );
152
147
return ps .executeUpdate ();
153
- } finally {
154
- try {
155
- ps .close ();
156
- } catch (SQLException e ) {
157
- //ignore
158
- }
159
148
}
160
149
}
161
150
@@ -179,15 +168,8 @@ public int delete(String sql, Object... args) throws SQLException {
179
168
* @throws SQLException If statement preparation or execution fails
180
169
*/
181
170
public void run (String sql ) throws SQLException {
182
- Statement stmt = connection .createStatement ();
183
- try {
171
+ try (Statement stmt = connection .createStatement ()) {
184
172
stmt .execute (sql );
185
- } finally {
186
- try {
187
- stmt .close ();
188
- } catch (SQLException e ) {
189
- //ignore
190
- }
191
173
}
192
174
}
193
175
@@ -221,43 +203,33 @@ private void setParameters(PreparedStatement ps, Object... args) throws SQLExcep
221
203
}
222
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