MySQL method to get the value of the self-increment ID

Source: Internet
Author: User

Native JDBC Mode:

Statement.getGeneratedKeys()

Example:

Statement stmt =NULL; ResultSet RS=NULL;Try {    //    //Create A Statement instance that we can use for//' normal ' result sets assuming you have a//Connection ' conn ' to a MySQL database already//availablestmt=conn.createstatement (); //    //Issue the DDL queries for the table for this example//Stmt.executeupdate ("DROP TABLE IF EXISTS autoinctutorial"); Stmt.executeupdate ("CREATE TABLE autoinctutorial (" + "Prikey INT not NULL auto_increment," + "DataField VARCHAR (64), PRIMARY KEY (Prikey)) "); //    //Insert one row that'll generate an AUTO INCREMENT//key in the ' Prikey ' field//Stmt.executeupdate ("INSERT into Autoinctutorial (DataField)" + "values (' Can I Get the Auto Increment Field? ')", Statement.return_generated_keys); //    //Example of using Statement.getgeneratedkeys ()//To retrieve the value of an auto-increment//value//    intAutoinckeyfromapi =-1; RS=Stmt.getgeneratedkeys (); if(Rs.next ()) {Autoinckeyfromapi= Rs.getint (1); } Else {        //throw an exception from here} System.out.println ("Key returned from Getgeneratedkeys ():" +autoinckeyfromapi);} finally {    if(rs! =NULL) {        Try{rs.close (); } Catch(SQLException ex) {//Ignore        }    }    if(stmt! =NULL) {        Try{stmt.close (); } Catch(SQLException ex) {//Ignore        }    }}

There are also useSELECT LAST_INSERT_ID() 注意:并发可能会出现问题。示例:

Statement stmt =NULL; ResultSet RS=NULL;Try {    //    //Create A Statement instance that we can use for//' normal ' result sets.stmt=conn.createstatement (); //    //Issue the DDL queries for the table for this example//Stmt.executeupdate ("DROP TABLE IF EXISTS autoinctutorial"); Stmt.executeupdate ("CREATE TABLE autoinctutorial (" + "Prikey INT not NULL auto_increment," + "DataField VARCHAR (64), PRIMARY KEY (Prikey)) "); //    //Insert one row that'll generate an AUTO INCREMENT//key in the ' Prikey ' field//Stmt.executeupdate ("INSERT into Autoinctutorial (DataField)" + "values (' Can I Get the Auto Increment Field? ')"); //    //Use the MySQL last_insert_id ()//function to do the same thing as Getgeneratedkeys ()//    intAutoinckeyfromfunc =-1; RS= Stmt.executequery ("Select last_insert_id ()"); if(Rs.next ()) {Autoinckeyfromfunc= Rs.getint (1); } Else {        //throw an exception from here} System.out.println ("Key returned from" + "' Select last_insert_id () ':" +autoinckeyfromfunc);} finally {    if(rs! =NULL) {        Try{rs.close (); } Catch(SQLException ex) {//Ignore        }    }    if(stmt! =NULL) {        Try{stmt.close (); } Catch(SQLException ex) {//Ignore        }    }}

The MyBatis package is configured as follows:

<id= "Insert"  parametertype= "Post"  usegeneratedkeys = "true" Keyproperty = "id" >

Call

postDao.add(post);

Returns 1 after the same result as before, using Post.getid () to obtain the self-increment ID.

Reference documents:

"1" http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-last-insert-id.html

"2" Http://stackoverflow.com/questions/12241260/get-auto-genearated-key-for-the-inserted-record-in-mybatis

MySQL method to get the value of the self-increment ID

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.