Skip to content

Commit 82f84df

Browse files
committed
solve PR requested
1 parent 58c72ec commit 82f84df

File tree

2 files changed

+45
-34
lines changed

2 files changed

+45
-34
lines changed

src/main/java/org/apache/ibatis/jdbc/ScriptRunner.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,6 @@ private void executeStatement(String command) throws SQLException {
260260
String message = "Error executing: " + command + ". Cause: " + e;
261261
printlnError(message);
262262
}
263-
} catch (Exception e) {
264-
// when close method throws some exception , it will catch by here .
265-
// keep the original behavior unchanged
266-
// see doc https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
267263
}
268264
}
269265
}

src/main/java/org/apache/ibatis/jdbc/SqlRunner.java

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/**
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.
1515
*/
1616
package org.apache.ibatis.jdbc;
1717

@@ -57,7 +57,9 @@ public void setUseGeneratedKeySupport(boolean useGeneratedKeySupport) {
5757
*
5858
* @param sql The SQL
5959
* @param args The arguments to be set on the statement.
60+
*
6061
* @return The row expected.
62+
*
6163
* @throws SQLException If less or more than one row is returned
6264
*/
6365
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
7375
*
7476
* @param sql The SQL
7577
* @param args The arguments to be set on the statement.
78+
*
7679
* @return The list of rows expected.
80+
*
7781
* @throws SQLException If statement preparation or execution fails
7882
*/
7983
public List<Map<String, Object>> selectAll(String sql, Object... args) throws SQLException {
8084
try (PreparedStatement ps = connection.prepareStatement(sql);) {
8185
setParameters(ps, args);
82-
ResultSet rs = ps.executeQuery();
83-
return getResults(rs);
86+
try (ResultSet rs = ps.executeQuery();) {
87+
return getResults(rs);
88+
}
8489
}
8590
}
8691

@@ -89,7 +94,9 @@ public List<Map<String, Object>> selectAll(String sql, Object... args) throws SQ
8994
*
9095
* @param sql The SQL
9196
* @param args The arguments to be set on the statement.
97+
*
9298
* @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
99+
*
93100
* @throws SQLException If statement preparation or execution fails
94101
*/
95102
public int insert(String sql, Object... args) throws SQLException {
@@ -104,17 +111,19 @@ public int insert(String sql, Object... args) throws SQLException {
104111
setParameters(ps, args);
105112
ps.executeUpdate();
106113
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+
}
118127
}
119128
}
120129
}
@@ -135,11 +144,13 @@ public int insert(String sql, Object... args) throws SQLException {
135144
*
136145
* @param sql The SQL
137146
* @param args The arguments to be set on the statement.
147+
*
138148
* @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
149+
*
139150
* @throws SQLException If statement preparation or execution fails
140151
*/
141152
public int update(String sql, Object... args) throws SQLException {
142-
try(PreparedStatement ps = connection.prepareStatement(sql);) {
153+
try (PreparedStatement ps = connection.prepareStatement(sql);) {
143154
setParameters(ps, args);
144155
return ps.executeUpdate();
145156
}
@@ -150,7 +161,9 @@ public int update(String sql, Object... args) throws SQLException {
150161
*
151162
* @param sql The SQL
152163
* @param args The arguments to be set on the statement.
164+
*
153165
* @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
166+
*
154167
* @throws SQLException If statement preparation or execution fails
155168
*/
156169
public int delete(String sql, Object... args) throws SQLException {
@@ -162,10 +175,11 @@ public int delete(String sql, Object... args) throws SQLException {
162175
* Good for DDL
163176
*
164177
* @param sql The SQL
178+
*
165179
* @throws SQLException If statement preparation or execution fails
166180
*/
167181
public void run(String sql) throws SQLException {
168-
try(Statement stmt = connection.createStatement();) {
182+
try (Statement stmt = connection.createStatement();) {
169183
stmt.execute(sql);
170184
}
171185
}
@@ -185,7 +199,8 @@ public void closeConnection() {
185199
private void setParameters(PreparedStatement ps, Object... args) throws SQLException {
186200
for (int i = 0, n = args.length; i < n; i++) {
187201
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");
189204
} else if (args[i] instanceof Null) {
190205
((Null) args[i]).getTypeHandler().setParameter(ps, i + 1, null, ((Null) args[i]).getJdbcType());
191206
} else {
@@ -200,7 +215,7 @@ private void setParameters(PreparedStatement ps, Object... args) throws SQLExcep
200215
}
201216

202217
/**
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
204219
*/
205220
private List<Map<String, Object>> getResults(ResultSet rs) throws SQLException {
206221
List<Map<String, Object>> list = new ArrayList<>();

0 commit comments

Comments
 (0)