From 8bf12fb775ee379ac9c26d3aded37f19a6257e75 Mon Sep 17 00:00:00 2001 From: hantmac Date: Thu, 7 Dec 2023 19:04:29 +0800 Subject: [PATCH] fix: executeUpdate bug --- .../jdbc/DatabendPreparedStatement.java | 26 +++++++++++++------ .../databend/jdbc/TestPrepareStatement.java | 13 ++++++++-- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java index ed0dfb2f..f64a757d 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java @@ -363,23 +363,33 @@ private List prepareSQL(@NonNull Map para @Override public boolean execute() throws SQLException { - return this.execute(prepareSQL(batchInsertUtils.get().getProvideParams())).isPresent(); + Boolean r; + try { + r = this.execute(prepareSQL(batchInsertUtils.get().getProvideParams())).isPresent(); + } catch (Exception e) { + throw new SQLException(e); + } finally { + clearBatch(); + } + return r; } protected Optional execute(List statements) throws SQLException { - Optional resultSet = Optional.empty(); + Optional optionalResultSet = Optional.empty(); + ResultSet r; try { for (int i = 0; i < statements.size(); i++) { - if (i == 0) { - internalExecute(statements.get(i).getSql(), null); - resultSet = Optional.ofNullable(getResultSet()); - } else { - internalExecute(statements.get(i).getSql(), null); + internalExecute(statements.get(i).getSql(), null); + r = getResultSet(); + optionalResultSet = Optional.ofNullable(r); + while (r != null && r.next()) { } } + } catch (Exception e) { + throw new SQLException(e); } finally { } - return resultSet; + return optionalResultSet; } @Override diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java index aba25ed6..503bd877 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestPrepareStatement.java @@ -8,7 +8,7 @@ import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; -import java.sql.PreparedStatement; +import java.sql.*; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -297,6 +297,15 @@ public void testPrepareStatementExecute() throws SQLException { Assertions.assertEquals(1, r.getLong("number")); System.out.println(r.getLong("number")); } + try { + Statement statement = conn.createStatement(); + ResultSet rs = statement.executeQuery("show processlist"); + while (rs.next()) { + System.out.println(rs.getString("id")); + } + } catch (Exception e) { + e.printStackTrace(); + } } @Test @@ -339,7 +348,7 @@ public void testAllPreparedStatement() throws SQLException { String deleteSQL = "delete from test_prepare_statement where a = ?"; try (PreparedStatement statement = conn.prepareStatement(deleteSQL)) { statement.setInt(1, 1); - int result = statement.executeUpdate(); + boolean result = statement.execute(); System.out.println(result); } ResultSet r3 = conn.createStatement().executeQuery("select * from test_prepare_statement");