在 Java 中插入 SQL 后访问自动递增标识字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/76254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access to auto increment identity field after SQL insert in Java
提问by Sean
Any advice on how to read auto-incrementing identity field assigned to newly created record from call through java.sql.Statement.executeUpdate
?
关于如何从调用读取分配给新创建记录的自动递增身份字段的任何建议java.sql.Statement.executeUpdate
?
I know how to do this in SQL for several DB platforms, but would like to know what database independent interfaces exist in java.sql
to do this, and any input on people's experience with this across DB platforms.
我知道如何在 SQL 中为多个数据库平台执行此操作,但想知道存在哪些数据库独立接口java.sql
来执行此操作,以及有关人们跨数据库平台使用此操作的经验的任何输入。
采纳答案by Daniel Spiewak
The following snibblet of code should do ya':
下面的代码应该可以做你':
PreparedStatement stmt = conn.prepareStatement(sql,
Statement.RETURN_GENERATED_KEYS);
// ...
ResultSet res = stmt.getGeneratedKeys();
while (res.next())
System.out.println("Generated key: " + res.getInt(1));
This is known to work on the following databases
已知这适用于以下数据库
- Derby
- MySQL
- SQL Server
- 德比
- MySQL
- 数据库服务器
For databases where it doesn't work (HSQLDB, Oracle, PostgreSQL, etc), you will need to futz with database-specific tricks. For example, on PostgreSQL you would make a call to SELECT NEXTVAL(...)
for the sequence in question.
对于它不起作用的数据库(HSQLDB、Oracle、PostgreSQL 等),您需要使用特定于数据库的技巧。例如,在 PostgreSQL 上,您将调用SELECT NEXTVAL(...)
有问题的序列。
Note that the parameters for executeUpdate(...)
are analogous.
请注意, 的参数executeUpdate(...)
是类似的。
回答by ScArcher2
I've always had to make a second call using query after the insert.
我总是不得不在插入后使用查询进行第二次调用。
You could use an ORM like hibernate. I think it does this stuff for you.
您可以使用像 hibernate 这样的 ORM。我认为它为你做这些事情。
回答by Alexandre Victoor
@ScArcher2 : I agree, Hibernate needs to make a second call to get the newly generated identity UNLESS an advanced generator strategy is used (sequence, hilo...)
@ScArcher2 :我同意,除非使用高级生成器策略(序列,hilo ...),否则 Hibernate 需要进行第二次调用以获取新生成的身份
回答by marcospereira
ResultSet keys = statement.getGeneratedKeys();
Later, just iterate over ResultSet.
稍后,只需迭代 ResultSet。
回答by Daniel Spiewak
@ScArcher2
@ScArcher2
Making a second call is extremelydangerous. The process of INSERT
ing and selecting the resultant auto-generated keys must be atomic, otherwise you may receive inconsistent results on the key select. Consider two asynchronous INSERT
s where they both complete before either has a chance to select the generated keys. Which process gets which list of keys? Most cross-database ORMs have to do annoying things like in-process thread locking in order to keep results deterministic. This is notsomething you want to do by hand, especially if you are using a database which does support atomic generated key retrieval (HSQLDB is the only one I know of which does not).
拨打第二个电话是极其危险的。的过程INSERT
荷兰国际集团和选择的结果自动生成的键必须是原子,否则你可能会在关键选择收到不一致的结果。考虑两个异步INSERT
s,它们都在有机会选择生成的键之前完成。哪个进程获取哪个键列表?大多数跨数据库 ORM 必须做一些烦人的事情,比如进程内线程锁定,以保持结果的确定性。这不是您想要手动完成的事情,特别是如果您使用的是支持原子生成密钥检索的数据库(HSQLDB 是我所知道的唯一一个不支持的数据库)。