JDBC gets the ID of the newly inserted auto-increment Column

Source: Internet
Author: User
Public void insert () {int autoinckeyfromapi =-1; // Step 1: Define autoinckeyfromapi to receive the value of ID; autoinckeyfromapi connection conn = dbconnectionmanager. getconnection (); preparedstatement PS = NULL; resultset rs = NULL; string SQL = NULL; SQL = "insert into tab_tsminfo values ()"; logger. debug (SQL); try {PS = Conn. preparestatement (SQL);/** add statement. return_generated_keys. Otherwise, an error is returned: * Java. SQL. sqlexception: generat Ed keys not requested. you need to * specify statement. return_generated_keys to * statement.exe cuteupdate () or connection. preparestatement (). * Put PS = conn on the Internet. preparestatement (SQL, statement. return_generated_keys); * However, if I try to put it in this line, an error will still be reported. If I put it in the next line, no error will be reported. I don't know why. */Ps.exe cuteupdate (SQL, statement. return_generated_keys); RS = ps. getgeneratedkeys (); If (RS. next () {system. out. print (RS. getint (1); // obtain the ID autoinckeyfromapi = rs of the inserted row. getint (1);} Rs. close (); RS = NULL; PS. close (); PS = NULL;} catch (sqlexception e) {// todo auto-generated Catch Block logger. debug (E. tostring ();} finally {If (RS! = NULL) {try {Rs. Close () ;}catch (exception e) {e. printstacktrace () ;}} if (PS! = NULL) {try {ps. Close () ;}catch (exception e) {e. printstacktrace () ;}} dbconnectionmanager. freeconnection (conn );}

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.