1
1
/**
2
- * Copyright 2009-2020 the original author or authors.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
2
+ * Copyright 2009-2020 the original author or authors.
3
+ * <p>
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ * <p>
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ * <p>
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
15
*/
16
16
package org .apache .ibatis .jdbc ;
17
17
@@ -57,7 +57,9 @@ public void setUseGeneratedKeySupport(boolean useGeneratedKeySupport) {
57
57
*
58
58
* @param sql The SQL
59
59
* @param args The arguments to be set on the statement.
60
+ *
60
61
* @return The row expected.
62
+ *
61
63
* @throws SQLException If less or more than one row is returned
62
64
*/
63
65
public Map <String , Object > selectOne (String sql , Object ... args ) throws SQLException {
@@ -73,14 +75,17 @@ public Map<String, Object> selectOne(String sql, Object... args) throws SQLExcep
73
75
*
74
76
* @param sql The SQL
75
77
* @param args The arguments to be set on the statement.
78
+ *
76
79
* @return The list of rows expected.
80
+ *
77
81
* @throws SQLException If statement preparation or execution fails
78
82
*/
79
83
public List <Map <String , Object >> selectAll (String sql , Object ... args ) throws SQLException {
80
84
try (PreparedStatement ps = connection .prepareStatement (sql );) {
81
85
setParameters (ps , args );
82
- ResultSet rs = ps .executeQuery ();
83
- return getResults (rs );
86
+ try (ResultSet rs = ps .executeQuery ();) {
87
+ return getResults (rs );
88
+ }
84
89
}
85
90
}
86
91
@@ -89,7 +94,9 @@ public List<Map<String, Object>> selectAll(String sql, Object... args) throws SQ
89
94
*
90
95
* @param sql The SQL
91
96
* @param args The arguments to be set on the statement.
97
+ *
92
98
* @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
99
+ *
93
100
* @throws SQLException If statement preparation or execution fails
94
101
*/
95
102
public int insert (String sql , Object ... args ) throws SQLException {
@@ -104,17 +111,19 @@ public int insert(String sql, Object... args) throws SQLException {
104
111
setParameters (ps , args );
105
112
ps .executeUpdate ();
106
113
if (useGeneratedKeySupport ) {
107
- List <Map <String , Object >> keys = getResults (ps .getGeneratedKeys ());
108
- if (keys .size () == 1 ) {
109
- Map <String , Object > key = keys .get (0 );
110
- Iterator <Object > i = key .values ().iterator ();
111
- if (i .hasNext ()) {
112
- Object genkey = i .next ();
113
- if (genkey != null ) {
114
- try {
115
- return Integer .parseInt (genkey .toString ());
116
- } catch (NumberFormatException e ) {
117
- //ignore, no numeric key support
114
+ try (ResultSet generatedKeys = ps .getGeneratedKeys ();) {
115
+ List <Map <String , Object >> keys = getResults (generatedKeys );
116
+ if (keys .size () == 1 ) {
117
+ Map <String , Object > key = keys .get (0 );
118
+ Iterator <Object > i = key .values ().iterator ();
119
+ if (i .hasNext ()) {
120
+ Object genkey = i .next ();
121
+ if (genkey != null ) {
122
+ try {
123
+ return Integer .parseInt (genkey .toString ());
124
+ } catch (NumberFormatException e ) {
125
+ //ignore, no numeric key support
126
+ }
118
127
}
119
128
}
120
129
}
@@ -135,11 +144,13 @@ public int insert(String sql, Object... args) throws SQLException {
135
144
*
136
145
* @param sql The SQL
137
146
* @param args The arguments to be set on the statement.
147
+ *
138
148
* @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
149
+ *
139
150
* @throws SQLException If statement preparation or execution fails
140
151
*/
141
152
public int update (String sql , Object ... args ) throws SQLException {
142
- try (PreparedStatement ps = connection .prepareStatement (sql );) {
153
+ try (PreparedStatement ps = connection .prepareStatement (sql );) {
143
154
setParameters (ps , args );
144
155
return ps .executeUpdate ();
145
156
}
@@ -150,7 +161,9 @@ public int update(String sql, Object... args) throws SQLException {
150
161
*
151
162
* @param sql The SQL
152
163
* @param args The arguments to be set on the statement.
164
+ *
153
165
* @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
166
+ *
154
167
* @throws SQLException If statement preparation or execution fails
155
168
*/
156
169
public int delete (String sql , Object ... args ) throws SQLException {
@@ -162,10 +175,11 @@ public int delete(String sql, Object... args) throws SQLException {
162
175
* Good for DDL
163
176
*
164
177
* @param sql The SQL
178
+ *
165
179
* @throws SQLException If statement preparation or execution fails
166
180
*/
167
181
public void run (String sql ) throws SQLException {
168
- try (Statement stmt = connection .createStatement ();) {
182
+ try (Statement stmt = connection .createStatement ();) {
169
183
stmt .execute (sql );
170
184
}
171
185
}
@@ -185,7 +199,8 @@ public void closeConnection() {
185
199
private void setParameters (PreparedStatement ps , Object ... args ) throws SQLException {
186
200
for (int i = 0 , n = args .length ; i < n ; i ++) {
187
201
if (args [i ] == null ) {
188
- throw new SQLException ("SqlRunner requires an instance of Null to represent typed null values for JDBC compatibility" );
202
+ throw new SQLException (
203
+ "SqlRunner requires an instance of Null to represent typed null values for JDBC compatibility" );
189
204
} else if (args [i ] instanceof Null ) {
190
205
((Null ) args [i ]).getTypeHandler ().setParameter (ps , i + 1 , null , ((Null ) args [i ]).getJdbcType ());
191
206
} else {
@@ -200,7 +215,7 @@ private void setParameters(PreparedStatement ps, Object... args) throws SQLExcep
200
215
}
201
216
202
217
/**
203
- * ResultSet should't was closed in here,it should be closed in caller
218
+ * ResultSet should't was closed in here,it should be closed in caller
204
219
*/
205
220
private List <Map <String , Object >> getResults (ResultSet rs ) throws SQLException {
206
221
List <Map <String , Object >> list = new ArrayList <>();
0 commit comments