0
点赞
收藏
分享

微信扫一扫

MySQL jdbc multi queries prepareStatement.getUpdateCount()返回值问题

蓝哆啦呀 2021-09-21 阅读 20
我的博客

jdbc url开启了allowMultiQuies=true, 在更新多条sql的时候需要拿到更新的总数,直接用ps.getUpdateCount()返回的是第一条sql的更新值,如果需要拿到总更新数目,需要用到这里方法:参考https://stackoverflow.com/questions/63573443/mysql-jdbc-multi-update-return-wrong-affected-rows

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;
        }
    }

循环调用preparedStatement.getMoreResults()preparedStatement.getUpdateCount()累加即可。

PS:其实这里拿到的并不是真正更新的条数,而是found rows, 想要拿到(前后值)真正发生变化的行数需要在jdbc url里加上useAffectedRows=true.
PS: 在log_format=row的情况下,前后值没发生变化的更新不记录binlog.
PS:用pstmt.addBatch(sql) & pstmt.executeBatch()返回的是int[]即多个sql更新的count.

举报

相关推荐

0 条评论