|
1 | 1 | /**
|
2 |
| - * Copyright 2009-2017 the original author or authors. |
| 2 | + * Copyright 2009-2018 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.
|
@@ -225,59 +225,70 @@ private boolean commandReadyToExecute(String trimmedLine) {
|
225 | 225 | }
|
226 | 226 |
|
227 | 227 | private void executeStatement(String command) throws SQLException {
|
228 |
| - boolean hasResults = false; |
229 | 228 | Statement statement = connection.createStatement();
|
230 |
| - statement.setEscapeProcessing(escapeProcessing); |
231 |
| - String sql = command; |
232 |
| - if (removeCRs) { |
233 |
| - sql = sql.replaceAll("\r\n", "\n"); |
234 |
| - } |
235 |
| - if (stopOnError) { |
236 |
| - hasResults = statement.execute(sql); |
237 |
| - if (throwWarning) { |
238 |
| - // In Oracle, CRATE PROCEDURE, FUNCTION, etc. returns warning |
239 |
| - // instead of throwing exception if there is compilation error. |
240 |
| - SQLWarning warning = statement.getWarnings(); |
241 |
| - if (warning != null) { |
242 |
| - throw warning; |
243 |
| - } |
| 229 | + try { |
| 230 | + statement.setEscapeProcessing(escapeProcessing); |
| 231 | + String sql = command; |
| 232 | + if (removeCRs) { |
| 233 | + sql = sql.replaceAll("\r\n", "\n"); |
244 | 234 | }
|
245 |
| - } else { |
246 | 235 | try {
|
247 |
| - hasResults = statement.execute(sql); |
| 236 | + boolean hasResults = statement.execute(sql); |
| 237 | + while (!(!hasResults && statement.getUpdateCount() == -1)) { |
| 238 | + checkWarnings(statement); |
| 239 | + printResults(statement, hasResults); |
| 240 | + hasResults = statement.getMoreResults(); |
| 241 | + } |
| 242 | + } catch (SQLWarning e) { |
| 243 | + throw e; |
248 | 244 | } catch (SQLException e) {
|
249 |
| - String message = "Error executing: " + command + ". Cause: " + e; |
250 |
| - printlnError(message); |
| 245 | + if (stopOnError) { |
| 246 | + throw e; |
| 247 | + } else { |
| 248 | + String message = "Error executing: " + command + ". Cause: " + e; |
| 249 | + printlnError(message); |
| 250 | + } |
| 251 | + } |
| 252 | + } finally { |
| 253 | + try { |
| 254 | + statement.close(); |
| 255 | + } catch (Exception e) { |
| 256 | + // Ignore to workaround a bug in some connection pools |
| 257 | + // (Does anyone know the details of the bug?) |
251 | 258 | }
|
252 | 259 | }
|
253 |
| - printResults(statement, hasResults); |
254 |
| - try { |
255 |
| - statement.close(); |
256 |
| - } catch (Exception e) { |
257 |
| - // Ignore to workaround a bug in some connection pools |
| 260 | + } |
| 261 | + |
| 262 | + private void checkWarnings(Statement statement) throws SQLException { |
| 263 | + if (!throwWarning) { |
| 264 | + return; |
| 265 | + } |
| 266 | + // In Oracle, CREATE PROCEDURE, FUNCTION, etc. returns warning |
| 267 | + // instead of throwing exception if there is compilation error. |
| 268 | + SQLWarning warning = statement.getWarnings(); |
| 269 | + if (warning != null) { |
| 270 | + throw warning; |
258 | 271 | }
|
259 | 272 | }
|
260 | 273 |
|
261 | 274 | private void printResults(Statement statement, boolean hasResults) {
|
262 |
| - try { |
263 |
| - if (hasResults) { |
264 |
| - ResultSet rs = statement.getResultSet(); |
265 |
| - if (rs != null) { |
266 |
| - ResultSetMetaData md = rs.getMetaData(); |
267 |
| - int cols = md.getColumnCount(); |
268 |
| - for (int i = 0; i < cols; i++) { |
269 |
| - String name = md.getColumnLabel(i + 1); |
270 |
| - print(name + "\t"); |
271 |
| - } |
272 |
| - println(""); |
273 |
| - while (rs.next()) { |
274 |
| - for (int i = 0; i < cols; i++) { |
275 |
| - String value = rs.getString(i + 1); |
276 |
| - print(value + "\t"); |
277 |
| - } |
278 |
| - println(""); |
279 |
| - } |
| 275 | + if (!hasResults) { |
| 276 | + return; |
| 277 | + } |
| 278 | + try (ResultSet rs = statement.getResultSet()) { |
| 279 | + ResultSetMetaData md = rs.getMetaData(); |
| 280 | + int cols = md.getColumnCount(); |
| 281 | + for (int i = 0; i < cols; i++) { |
| 282 | + String name = md.getColumnLabel(i + 1); |
| 283 | + print(name + "\t"); |
| 284 | + } |
| 285 | + println(""); |
| 286 | + while (rs.next()) { |
| 287 | + for (int i = 0; i < cols; i++) { |
| 288 | + String value = rs.getString(i + 1); |
| 289 | + print(value + "\t"); |
280 | 290 | }
|
| 291 | + println(""); |
281 | 292 | }
|
282 | 293 | } catch (SQLException e) {
|
283 | 294 | printlnError("Error printing results: " + e.getMessage());
|
|
0 commit comments