MySQL get just insert row ID Rollup

Source: Internet
Author: User

MySQL get just insert row ID Rollup

When we write a database program, we often need to get the maximum number of numbers in a table,

In general, it is possible to get the ID of the data just inserted, using SELECT Max (ID) from table.
But in the case of multithreading, it is not.

Here are three ways to

(1) Getgeneratedkeys () method:

Program Snippet:

Connection conn =;
Serializable ret = null;
PreparedStatement state =.;
ResultSet Rs=null;
try {
State.executeupdate ();
rs = State.getgeneratedkeys ();
if (Rs.next ()) {
ret = (Serializable) rs.getobject (1);
}
} catch (SQLException e) {
}
return ret;

(2) LAST_INSERT_ID:

LAST_INSERT_ID is not related to table, last_insert_id will change if data is inserted into table A, and then the data is injected into form B.

Max (ID) is obviously not available in the case where multiple users are alternately inserting data.
This should use last_insert_id, because last_insert_id is connection based, as long as each thread uses a separate connection object, Last_insert_ The ID function returns the ID of the first record generated by the connection for the latest insert or update* of the auto_increment column. This value cannot be affected by other clients (Connection), which guarantees that you will be able to retrieve your ID without worrying about other clients ' activities and do not need to lock. Insert multiple records using a single INSERT statement, LAST_INSERT_ID returns a list.

(3) SELECT @ @IDENTITY:

String sql= "SELECT @ @IDENTITY";

@ @identity is a system-defined global variable that represents the last time the value of the self-increment column for inserting data into a table with the identity attribute (that is, the self-increment column). General system-defined global variables start with @@ 开头 and user-defined variables begin with @. For example, there is a table A, its self-increment column is an ID, when inserting a row of data into a table, if the value of the self-increment after inserting data automatically increases to 101, then the value obtained by the SELECT @ @identity is 101. The @ @identity is used only if the connection is not closed when the insert operation is executed, or a null value is obtained.

MySQL get just insert row ID Rollup

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.