diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractStatement.java index a801f5a674acdd23f1ca7f949cbb7092f4633bda..12641087fb774a82e80c8339f752ff5f514524a0 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractStatement.java @@ -9,6 +9,7 @@ public abstract class AbstractStatement extends WrapperImpl implements Statement protected List batchedArgs; private int fetchSize; + protected int affectedRows = -1; @Override public abstract ResultSet executeQuery(String sql) throws SQLException; @@ -247,6 +248,7 @@ public abstract class AbstractStatement extends WrapperImpl implements Statement public boolean getMoreResults(int current) throws SQLException { if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); + this.affectedRows = -1; switch (current) { case Statement.CLOSE_CURRENT_RESULT: return false; diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java index e1ebc4ab3cf498168181dbea08a3ac28194a5c7d..436bdcf582b821292c5f4e69f51688f9bf84b870 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java @@ -23,7 +23,6 @@ public class TSDBStatement extends AbstractStatement { * Status of current statement */ private boolean isClosed; - private int affectedRows = -1; private TSDBConnection connection; private TSDBResultSet resultSet; @@ -80,12 +79,13 @@ public class TSDBStatement extends AbstractStatement { if (isClosed()) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); } - + // execute query long pSql = this.connection.getConnector().executeQuery(sql); // if pSql is create/insert/update/delete/alter SQL if (this.connection.getConnector().isUpdateQuery(pSql)) { - this.affectedRows = this.connection.getConnector().getAffectedRows(pSql); + int rows = this.connection.getConnector().getAffectedRows(pSql); + this.affectedRows = rows == 0 ? -1 : this.connection.getConnector().getAffectedRows(pSql); this.connection.getConnector().freeResultSet(pSql); return false; } @@ -99,7 +99,7 @@ public class TSDBStatement extends AbstractStatement { if (isClosed()) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); } - + return this.resultSet; } @@ -113,14 +113,14 @@ public class TSDBStatement extends AbstractStatement { if (isClosed()) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); } - + if (this.connection.getConnector() == null) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); } - + return this.connection; } - + public void setConnection(TSDBConnection connection) { this.connection = connection; } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java index 21c76f73b287e55ef14f5d70cf6a911a9cb543db..b7f5fe8006368295753a366aa218a6cc17aa0588 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/rs/RestfulStatement.java @@ -22,7 +22,6 @@ public class RestfulStatement extends AbstractStatement { private final RestfulConnection conn; private volatile RestfulResultSet resultSet; - private volatile int affectedRows; public RestfulStatement(RestfulConnection conn, String database) { this.conn = conn; @@ -118,7 +117,7 @@ public class RestfulStatement extends AbstractStatement { throw TSDBError.createSQLException(resultJson.getInteger("code"), resultJson.getString("desc")); } this.resultSet = new RestfulResultSet(database, this, resultJson); - this.affectedRows = 0; + this.affectedRows = -1; return resultSet; } @@ -140,9 +139,10 @@ public class RestfulStatement extends AbstractStatement { if (head.size() != 1 || !"affected_rows".equals(head.getString(0))) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE); JSONArray data = jsonObject.getJSONArray("data"); - if (data != null) - return data.getJSONArray(0).getInteger(0); - + if (data != null) { + int rows = data.getJSONArray(0).getInteger(0); + return rows == 0 ? -1 : data.getJSONArray(0).getInteger(0); + } throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE); }