Don't say so much first, put the Java code out.
Public Static voidMain (string[] args)throwsinterruptedexception, IOException {Try{Drivermanagerdatasource DataSource=NewDrivermanagerdatasource (); Datasource.setdriverclassname ("Com.mysql.jdbc.Driver"); Datasource.seturl ("JDBC:MYSQL://172.23.88.107:3306/TEST?USEUNICODE=TRUE&CHARACTERENCODING=UTF-8&SERVERTIMEZONE=UTC"); Datasource.setusername ("Root"); Datasource.setpassword ("Zdsoft"); JdbcTemplate JdbcTemplate=NewJdbcTemplate (DataSource); intres= jdbctemplate.update ("Update student set Name= ' John Doe ', age=12 where id=25"); System.out.println (RES); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }
We know. In MySQL, if we modify the original value of a piece of data and the modified value is consistent, then the number of rows affected will be returned to 0
Well, then executing the above Java code will theoretically return 0. However, after many executions and multiple tests, the return is always 1. This makes people very puzzled, and later looked at the relevant information, the original JDBC returned by default is the number of matching rows (not the number of rows affected). So if we want to return the number of rows affected, we can add useaffectedrows=true to the URL connection address.
Url:jdbc:mysql://172.23.88.107:3306/test?useunicode=true&characterencoding=utf-8&servertimezone=utc &useaffectedrows=true
PS: In fact, SQL Server and Oracle do not have a matching number of rows and the number of rows affected, the return is the number of rows affected, and the number of rows affected by the MySQL match row number of values are consistent. Therefore, in order to ensure the consistency of three kinds of databases, it is generally not recommended to add useaffectedrows=true
Java access to mysql,update data return values through JDBC thinking