MySQL & Java - 获取最后插入值的 id (JDBC)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4246646/
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
MySQL & Java - Get id of the last inserted value (JDBC)
提问by markzzz
Possible Duplicate:
How to get the insert ID in JDBC?
可能的重复:
如何在 JDBC 中获取插入 ID?
Hi, I'm using JDBC to connect on database through out Java.
嗨,我正在使用 JDBC 通过 Java 连接数据库。
Now, I do some insert query, and I need to get the id of last inserted value (so, after a stmt.executeUpdate
).
现在,我进行了一些插入查询,我需要获取最后插入值的 id(因此,在 a 之后stmt.executeUpdate
)。
I don't need something like SELECT id FROM table ORDER BY id DESC LIMIT 1
, because I may have concurrency problems.
我不需要类似的东西SELECT id FROM table ORDER BY id DESC LIMIT 1
,因为我可能有并发问题。
I Just need to retrieve the id associated to the last insertion (about my instance of the Statement).
我只需要检索与最后一次插入相关联的 id(关于我的 Statement 实例)。
I tried this, but seems it doesn't work on JDBC :
我试过这个,但似乎它在 JDBC 上不起作用:
public Integer insertQueryGetId(String query) {
Integer numero=0;
Integer risultato=-1;
try {
Statement stmt = db.createStatement();
numero = stmt.executeUpdate(query);
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()){
risultato=rs.getInt(1);
}
rs.close();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
errore = e.getMessage();
risultato=-1;
}
return risultato;
}
In fact, every time risultato = -1
, and I get java.sql.SQLException: Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate() or Connection.prepareStatement().
事实上,每一次risultato = -1
,我都得到java.sql.SQLException: Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate() or Connection.prepareStatement().
How can I fix this problem? Thanks Stackoverflow People :)
我该如何解决这个问题?感谢 Stackoverflow 人 :)
采纳答案by Sean Bright
Wouldn't you just change:
你不会只是改变:
numero = stmt.executeUpdate(query);
to:
到:
numero = stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
Take a look at the documentation for the JDBC Statement
interface.
查看 JDBCStatement
接口的文档。
Update: Apparently there is a lot of confusion about this answer, but my guess is that the people that are confused are not reading it in the context of the question that was asked. If you take the code that the OP provided in his question and replace the single line (line 6) that I am suggesting, everything will work. The numero
variable is completely irrelevant and its value is never read after it is set.
更新:显然这个答案有很多困惑,但我的猜测是那些感到困惑的人并没有在提出的问题的上下文中阅读它。如果您采用 OP 在他的问题中提供的代码并替换我建议的单行(第 6 行),一切都会正常进行。该numero
变量是完全不相关的,它的值在设置后永远不会被读取。
回答by Buhake Sindi
Alternatively you can do:
或者你可以这样做:
Statement stmt = db.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
numero = stmt.executeUpdate();
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()){
risultato=rs.getString(1);
}
But use Sean Bright's answer instead for your scenario.
但是对于您的场景,请改用 Sean Bright 的答案。