JDBC返回的受影响的行数错误 [英] JDBC returns wrong number of affected rows

查看:338
本文介绍了JDBC返回的受影响的行数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有一个包含10行的表,并且执行两次此查询,则第二次获得的错误行数将是错误的.

If we have a table with 10 rows and we execute this query twice we get the wrong number of affected rows the second time.

Statement st = open();

st.executeUpdate("UPDATE  `tickets` SET price=1000"); // return 10
st.executeUpdate("UPDATE  `tickets` SET price=1000"); // return 10

显然这是不正确的,因为在第一个查询中所有行的价格都更新为1000,而在第二个查询中实际上没有任何变化,但是又返回了10!

Obviously it isn't correct because in first query the price of all rows is updated to 1000 and in the second query nothing actually changes but it returns 10 again!

如何获取实际更新的行数?

How can I get the number of rows that are actually updated?

推荐答案

JDBC规范显然要求驱动程序使executeUpdate()通过UPDATE语句返回 found 的行数,而不是排实际受影响.

The JDBC specification apparently dictates that drivers have executeUpdate() return the number of rows found by the UPDATE statement, not the number of rows actually affected.

要使MySQL Connector/J返回实际更改的行数,您可以将属性useAffectedRows=true添加到连接URL中,尽管

To have MySQL Connector/J return the number of rows actually changed you can add the property useAffectedRows=true to your connection URL, although the documentation does warn that it is

不兼容JDBC,将破坏大多数依赖找到的"行与受影响的行"的DML语句的应用程序

not JDBC-compliant, will break most applications that rely on "found" rows vs. "affected rows" for DML statements

这篇关于JDBC返回的受影响的行数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆