英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论