While insert ing a SQL query we do not specify the auto increment values, if any.插入荷蘭SQL查詢,我們不指定自動增加值,如果有的話。 However they are often required for further processing.不過,他們往往需要作進一步處理。 Here is how you can obtain auto increment / auto generated values after a successful SQL INSERT statement.在這裡,是如何可以獲取自動遞增/自動產生的價值觀後,一個成功的SQL插入聲明。

 //     // 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(); / / / /插入一列,將產生一個自動遞增/ /關鍵在主鍵字段/ / stmt.executeupdate ( “插入autoinctest (評論) ” + “值( '我怎樣才能獲得自動遞增字段值? ' ) “ , statement.return_generated_keys ) ; / / / /使用statement.getgeneratedkeys ( ) / /檢索的價值( ) / /詮釋autoincvalue = -1 ;盧比= stmt.getgeneratedkeys ( ) ;如果( rs.next ( ) ) ( autoincvalue = rs.getint ( 1 ) ; )否則( / /錯誤) rs.close ( ) ; 

You can also specify and fetch multiple auto-increment key values.您也可以指定和市值多個自動增量關鍵的價值觀。 This is not just a convenience but a necessity where the primary key is also the auto increment key.這不僅是方便,而是一種需要的地方,小學的關鍵,也是自動遞增的關鍵。