While insert ing a SQL query we do not specify the auto increment values, if any. Enquanto ing inserir uma consulta SQL nós não especificar os valores auto incremento, se for o caso. However they are often required for further processing. No entanto, são muitas vezes necessários para o tratamento posterior. Here is how you can obtain auto increment / auto generated values after a successful SQL INSERT statement. Aqui está como você pode obter auto incremento / auto valores gerados após uma bem sucedida SQL INSERT declaração.

 //     // 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(); / / / / Inserir uma linha que vai gerar um incremento AUTO / / chave no campo chave primária / / stmt.executeUpdate ( "INSERT INTO autoIncTest (comentário)" + "valores (" Como eu posso obter o auto incremento campo valor? ') ", Statement.RETURN_GENERATED_KEYS), / / / / Use Statement.getGeneratedKeys () / / para recuperar o valor (s) / / int autoIncValue = -1; rs = stmt.getGeneratedKeys (); se (rs.next ( )) (AutoIncValue = rs.getInt (1);) mais (/ / erro) rs.close (); 

You can also specify and fetch multiple auto-increment key values. Você também pode especificar múltiplas e buscar auto incremento valores fundamentais. This is not just a convenience but a necessity where the primary key is also the auto increment key. Este não é apenas uma conveniência, mas uma necessidade quando a chave primária é também a chave auto incremento.