Java access to mysql,update data return values through JDBC thinking

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.