Java: How To Get Auto Increment Values After SQL Insert Java: Come arrivare auto di incremento dei valori dopo SQL Inserire
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.
Filed under Elencato sotto Database Banca dati , HSQLDB , Headline News Headline News , How To Come , J2EE , Java Software Software Java , Programming Programmazione , RDBMS | |
| |
RSS 2.0 RSS 2,0 | |
Trackback this Article | questo articolo |
Email this Article Invia questo articolo
You may also like to read Si può anche leggere come |




June 11th, 2007 at 8:42 pm 11 giugno 2007 alle 8:42 pm
This doesn’t work with postgres (at least, when I last tried it). Questo non funziona con postgres (almeno, quando ho provato ultimo). Instead, I use: Invece, io uso:
PreparedStatement insert = conn.prepareStatement(”insert into thetable (owner, created) values (?,?); select currval(’thetable_id_seq’)”; PreparedStatement inserire = conn.prepareStatement ( "inserire in thetable (proprietario, creato) valori (?,?); selezionare currval ( 'thetable_id_seq')";
…
insert.execute(); insert.execute ();
if (insert.getUpdateCount() == 1 && insert.getMoreResults()) { if (insert.getUpdateCount () == 1 & & insert.getMoreResults ()) (
ResultSet res = insert.getResultSet(); ResultSet res = insert.getResultSet ();
int id = res.getInt(1); int id = res.getInt (1);
} )
June 12th, 2007 at 12:34 am 12 giugno 2007 alle 12:34 am
Thanks for sharing. Grazie per la condivisione. I have tested my code on MySQL. Ho testato il mio codice a MySQL.