How to obtain the inserted auto-increment ID in MySQL

Source: Internet
Author: User

I briefly summarized the methods I currently know.

  1. Package
    Test;
  2. Import
    Java. SQL. connection;
  3. Import
    Java. SQL. drivermanager;
  4. Import
    Java. SQL. resultset;
  5. Import
    Java. SQL. statement;
  6. /**
  7. * Three Methods to automatically generate a primary key are available.
  8. *
  9. * @ Author Zhao Xueqing www.java2000.net
  10. *
  11. */
  12. Public
    Class
    Testgetpk {
  13. Public
    Static
    Void
    Main (string [] ARGs) throws
    Exception {
  14. Class. forname ("com. GBASE. JDBC. Driver"
    );
  15. String url = "JDBC: GBASE: // localhost/mytest"
    ;
  16. Connection con = drivermanager. getconnection (URL, "root"
    , 111111"
    );
  17. System. Out. println (getpk1 (CON ));
  18. System. Out. println (getpk2 (CON ));
  19. System. Out. println (getpk3 (CON ));
  20. }
  21. /**
  22. * Use getgeneratedkeys provided by JDBC 3.0. Recommended
  23. *
  24. * @ Param con
  25. * @ Return
  26. * @ Throws exception
  27. */
  28. Public
    Static
    Long
    Getpk1 (connection con) throws
    Exception {
  29. Statement stmt = con. createstatement ();
  30. Stmt.exe cuteupdate ("insert into t_type (name) values ('Can I get the auto increment field? ')"
    ,
  31. Statement. return_generated_keys );
  32. Int
    Autoinckeyfromapi =-1
    ;
  33. Resultset rs = stmt. getgeneratedkeys ();
  34. If
    (Rs. Next ()){
  35. Autoinckeyfromapi = Rs. getint (1
    );
  36. }
  37. Return
    Autoinckeyfromapi;
  38. }
  39. /**
  40. * Use your own special SQL.
  41. *
  42. * @ Param con
  43. * @ Return
  44. * @ Throws exception
  45. */
  46. Public
    Static
    Long
    Getpk2 (connection con) throws
    Exception {
  47. Statement stmt = con. createstatement ();
  48. Stmt.exe cuteupdate ("insert into t_type (name) values ('Can I get the auto increment field? ')"
    ,
  49. Statement. return_generated_keys );
  50. Int
    Autoinckeyfromfunc =-1
    ;
  51. Resultset rs = stmt.exe cutequery (

    "Select last_insert_id ()"
    );

  52. If
    (Rs. Next ()){
  53. Autoinckeyfromfunc = Rs. getint (1
    );
  54. }
  55. Return
    Autoinckeyfromfunc;
  56. }
  57. /**
  58. * Use updatable result sets.
  59. *
  60. * @ Param con
  61. * @ Return
  62. * @ Throws exception
  63. */
  64. Public
    Static
    Long
    Getpk3 (connection con) throws
    Exception {
  65. Statement stmt = con. createstatement (Java. SQL. resultset. type_forward_only,
  66. Java. SQL. resultset. concur_updatable );
  67. Resultset rs = stmt.exe cutequery ("select * From t_type"
    );
  68. Rs. movetoinsertrow ();
  69. Rs. updatestring ("name"
    , "Auto increment here? "
    );
  70. Rs. insertrow ();
  71. Rs. Last ();
  72. Int
    Autoinckeyfromrs = Rs. getint ("ID"
    );
  73. Return
    Autoinckeyfromrs;
  74. }
  75. }

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.