While insert ing a SQL query we do not specify the auto increment values, if any. Mentre ne inserire una query SQL non specificare l'auto di incremento dei valori, se del caso. However they are often required for further processing. Tuttavia sono spesso necessarie per l'ulteriore lavorazione. Here is how you can obtain auto increment / auto generated values after a successful SQL INSERT statement. Ecco come potete ottenere auto di incremento / valori generati automaticamente dopo un successo di SQL INSERT.

 //     // Insert one row that will generate an AUTO INCREMENT     // key in the primary key field     //     stmt.executeUpdate(             "INSERT INTO autoIncTest (comment) "             + "values ('How can I get the auto increment field value?')",             Statement.RETURN_GENERATED_KEYS);      //     // Use Statement.getGeneratedKeys()     // to retrieve the value(s)     //      int autoIncValue = -1;      rs = stmt.getGeneratedKeys();      if (rs.next()) {         autoIncValue = rs.getInt(1);     } else {          // Error     }      rs.close(); / / / / Inserire una riga che genereranno un auto di incremento / / chiave nel campo chiave primaria / / stmt.executeUpdate ( "INSERT INTO autoIncTest (commento)" + "VALUES ( 'Come posso ottenere il settore auto di incremento del valore? ') ", Statement.RETURN_GENERATED_KEYS); / / / / Usa Statement.getGeneratedKeys () / / per recuperare il valore (s) / / int autoIncValue = -1; rs = stmt.getGeneratedKeys (); if (rs.next ( )) (AutoIncValue = rs.getInt (1);) else (/ / Errore) rs.close (); 

You can also specify and fetch multiple auto-increment key values. È inoltre possibile specificare e recuperare più auto-incremento valori chiave. This is not just a convenience but a necessity where the primary key is also the auto increment key. Non è solo una comodità ma una necessità in cui la chiave primaria è anche la chiave di auto di incremento.