Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

mysql - JDBC returns wrong number of affected rows

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...