Javaweb Learning Summary (37)--Get the MySQL database auto-generated primary key

Source: Internet
Author: User

Javaweb Learning Summary (37)--Get the MySQL database auto-generated primary key

The test script is as follows:

1  CREATE TABLE test12 (3      ID int primary KEY auto_increment,4      name varchar (20) 5);

Test code:

1 package Me.gacl.demo; 2  3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import Me.gacl.ut Ils. Jdbcutils; 7  8 public class Test {9 public     static void Main (string[] args) {         Connection conn = null;11         Preparedsta Tement st = null;12         ResultSet rs = null;13         try{14             conn = Jdbcutils.getconnection ();             String sql = "Inser T into test1 (name) VALUES (?) "; +             st = conn.preparestatement (SQL),             st.setstring (1, "AAA"),             st.executeupdate ();             Gets the database auto-generated primary key             rs = St.getgeneratedkeys ();             if (Rs.next ()) {System.out.println (                 rs.getint (1)); 23             }24         }catch (Exception e) {             e.printstacktrace ();}finally{27 jdbcutils.release             (conn, St, RS);         }29     }30}

Javaweb Learning Summary (37)--Get the primary key automatically generated by MySQL database

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.