MySQL JDBC多重更新返回错误的受影响行数。

huangapple go评论85阅读模式
英文:

mysql jdbc multi update return wrong affected rows

问题

String sql = "update t_test set createDate = now() where id = 1; update t_test set createDate = now() where id = 101;";
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false";
Connection connection = DriverManager.getConnection(url, "username", "password");
connection.setAutoCommit(false);
final PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.execute();
//return 1, but 2 rows actually updated
System.out.println("updated rows=" + preparedStatement.getUpdateCount());
connection.commit();
connection.close();

table t_test contains two columns: id and createDate, I set allowMultiQueries=true in jdbc url.

In the above sql, two rows are updated, but preparedStatement.getUpdateCount() returns 1.

MySQL server version is 5.7, I have tested with jdbc driver version 5.1.40, 6.0.6, and 8.0.21, they all return the same result: 1

EDIT:
As @Massimo said, I use getMoreResults() and getUpdateCount() repeatedly to get the updated count and sum them up until getUpdateCount() returns -1.

Code is below:

System.out.println("updated rows=" + preparedStatement.getUpdateCount());
int totalCount = preparedStatement.getUpdateCount();
while (true){
    preparedStatement.getMoreResults();
    final int updateCount = preparedStatement.getUpdateCount();
    if(updateCount != -1){
        System.out.println("updated rows=" + preparedStatement.getUpdateCount());
        totalCount += preparedStatement.getUpdateCount();
    }else{
        break;
    }
}
System.out.println("total updated rows=" + totalCount);
英文:
    String sql = "update t_test set createDate = now() where id = 1; update t_test set createDate = now() where id = 101;";
    String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false";
    Connection connection = DriverManager.getConnection(url, "username", "password");
    connection.setAutoCommit(false);
    final PreparedStatement preparedStatement = connection.prepareStatement(sql);
    preparedStatement.execute();
    //return 1, but 2 rows actually updated
    System.out.println("updated rows=" + preparedStatement.getUpdateCount());
    connection.commit();
    connection.close();

table t_test contains two columns: id and createDate, I set allowMultiQueries=true in jdbc url.
In the above sql, two rows are updated, but preparedStatement.getUpdateCount() returns 1.
MySQL server version is 5.7, I have tested with jdbc driver version 5.1.40, 6.0.6 and 8.0.21, they all return same result:1

EDIT:
As @Massimo said, I use getMoreResults() and getUpdateCount() repeatedly to get updated count and sum them up until getUpdateCount() returns -1.

Code is below:

    System.out.println("updated rows=" + preparedStatement.getUpdateCount());
    int totalCount = preparedStatement.getUpdateCount();
    while (true){
        preparedStatement.getMoreResults();
        final int updateCount = preparedStatement.getUpdateCount();
        if(updateCount != -1){
            System.out.println("updated rows=" + preparedStatement.getUpdateCount());
            totalCount += preparedStatement.getUpdateCount();
        }else{
            break;
        }
    }
    System.out.println("total updated rows=" + totalCount);

答案1

得分: 1

Cannot test it.
However in theory, as you are executing multiple statements, the preparedStatement gets back the updateCount of each one.
So it's giving the update count of the first statement. To get the update count of the second you should try calling preparedStatement.getMoreResults() and then preparedStatement.getUpdateCount() again.

see docs

https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#getUpdateCount/

https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#getMoreResults/

英文:

Cannot test it.
However in theory, as you are executing multiple statements, the preparedStatement gets back the updateCount of each one.
So it's giving the update count of the first statement. To get the update count of the second you should try calling preparedStatement.getMoreResults() and then preparedStatement.getUpdateCount() again.

see docs

https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#getUpdateCount()

https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#getMoreResults()

huangapple
  • 本文由 发表于 2020年8月25日 14:51:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63573443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定