This code is used to solve the problem of automatically generated IDs after inserting data to the auto-increment table in JDBC. I searched for documents online for half a day. I used to provide methods in JDK. Click the link to open the references. Thank you for your guidance. Run the Code directly: *** get the auto-incrementing ID * @ paramsql * @ return * p after the auto-incrementing primary key is inserted.
This code is used to solve the problem of automatically generated IDs after inserting data to the auto-increment table in JDBC. I searched for documents online for half a day. I used to provide methods in JDK. Click the link to open the references. Thank you for your guidance. Run the Code directly:/*** get the auto-incrementing ID after the primary key is inserted * @ param SQL * @ return */p
This code is used to solve the problem of automatically generated IDs after inserting data to the auto-increment table in JDBC. I searched for documents online for half a day. I used to provide methods in JDK.
Click the link to open the references. Thank you for your guidance.
Go directly to the Code:
/*** Get the auto-incrementing ID after the primary key is inserted * @ param SQL * @ return */public int insertIntoDB (String SQL) {Connection conn = null; statement state = null; ResultSet rs = null; int key =-1; try {conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/jx3", "root", "root"); state = conn.createstatement(;;state.exe cuteUpdate (SQL, Statement. RETURN_GENERATED_KEYS); rs = state. getGeneratedKeys (); if (rs. next () {key = rs. getInt (1);} return key;} catch (SQLException e) {e. printStackTrace (); return key;} finally {try {if (rs! = Null) {rs. close (); rs = null;} if (state! = Null) {state. close (); state = null;} if (conn! = Null) {conn. close (); conn = null ;}} catch (SQLException e) {e. printStackTrace ();}}}