Java: How To Get Auto Increment Values After SQL Insert Java: Como obter a auto incremento valores Após inserir SQL
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.
Filed under Arquivado em Database Banco de dados , De HSQLDB , De Headline News Headline News , De How To How To , De J2EE , De Java Software Java Software , De Programming Programação , De RDBMS | |
| |
RSS 2.0 RSS 2,0 | |
Trackback this Article | este artigo |
Email this Article E-mail este artigo
You may also like to read Você pode também gosta de ler |





































June 11th, 2007 at 8:42 pm 11 de junho de 2007 em 8:42 pm
This doesn’t work with postgres (at least, when I last tried it). Isto não funciona com postgres (pelo menos, quando eu tentei-o último). Instead, I use: Em vez disso, eu uso:
PreparedStatement insert = conn.prepareStatement(”insert into thetable (owner, created) values (?,?); select currval(’thetable_id_seq’)”; PreparedStatement inserir = conn.prepareStatement ( "inserir em thetable (proprietário, criado) (?,?); selecionar valores 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 insert.getResultSet res = ();
int id = res.getInt(1); int id = res.getInt (1);
} )
June 12th, 2007 at 12:34 am 12 de junho de 2007 em 12:34 am
Thanks for sharing. Obrigado por compartilhar. I have tested my code on MySQL. Eu testei o meu código em MySQL.