Mysql aotu_increment is often used in master-slave relationships. MySQL does not have a specific sequence of oracle. In addition, the previous project used select max (*) to implement this function. Later, compared to the max (*) method, it was necessary to select the database more than once, and the efficiency was definitely not as good as that of using java.
Method 1. mysql has select LAST_INSERT_ID () to obtain the final auto_increment. Select last_insert_id () can be used to replace max (*). In this way, the auto_increment field can be set in the data table. In the case of multi-threaded concurrent operations, this method has a great advantage over max.
Method 2: Use java to provide getGeneratedKeys to obtain the final increment ID.
The code snippet is as follows:
Pstmt.exe cuteUpdate ();
Conn. commit ();
// Obtain auto_increment
Int aotuId;
Rs = pstmt. getGeneratedKeys ();
While (rs. next ()){
AutoId = rs. getInt (1 );
}
Author's "Practice record"