Java 插入行jdbc的主键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/201887/
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
Primary key from inserted row jdbc?
提问by Omar Kooheji
Is there a cross database platform way to get the primary key of the record you have just inserted?
有没有跨数据库平台的方式来获取刚刚插入的记录的主键?
I noted that this answersays that you can get it by Calling SELECT LAST_INSERT_ID()
and I think that you can call SELECT @@IDENTITY AS 'Identity';
is there a common way to do this accross databases in jdbc?
我注意到这个答案说你可以通过调用得到它SELECT LAST_INSERT_ID()
,我认为你可以调用SELECT @@IDENTITY AS 'Identity';
在 jdbc 中跨数据库执行此操作的通用方法吗?
If not how would you suggest I implement this for a piece of code that could access any of SQL Server, MySQL and Oracle?
如果不是,您如何建议我为一段可以访问 SQL Server、MySQL 和 Oracle 中的任何一个的代码实现这个?
采纳答案by extraneon
Copied from my code:
从我的代码复制:
pInsertOid = connection.prepareStatement(INSERT_OID_SQL, Statement.RETURN_GENERATED_KEYS);
where pInsertOid is a prepared statement.
其中 pInsertOid 是准备好的语句。
you can then obtain the key:
然后您可以获取密钥:
// fill in the prepared statement and
pInsertOid.executeUpdate();
ResultSet rs = pInsertOid.getGeneratedKeys();
if (rs.next()) {
int newId = rs.getInt(1);
oid.setId(newId);
}
Hope this gives you a good starting point.
希望这能给你一个好的起点。
回答by anjanb
for oracle, Hibernate uses NEXT_VALUE from a sequence if you have mapped a sequence for PKEY value generation.
对于 oracle,如果您已映射序列以生成 PKEY 值,则 Hibernate 使用序列中的 NEXT_VALUE。
Not sure what it does for MySQL or MS SQL server
不确定它对 MySQL 或 MS SQL 服务器有什么作用
回答by johnstok
Have you tried the Statement.executeUpdate()and Statement.getGeneratedKeys()methods? There is a developerWorks articlethat mentions the approach.
您是否尝试过Statement.executeUpdate()和Statement.getGeneratedKeys()方法?有一篇developerWorks 文章提到了这种方法。
Also, in JDBC 4.0 Sun added the row_id featurethat allows you to get a unique handle on a row. The feature is supported by Oracle and DB2. For sql server you will probably need a third party driver such as this one.
此外,在 JDBC 4.0 中,Sun 添加了row_id 功能,允许您获得行的唯一句柄。Oracle 和 DB2 支持该特性。对于 sql server,您可能需要第三方驱动程序,例如此驱动程序。
Good luck!
祝你好运!
回答by Dónal
Spring provides some useful support for this operationand the reference guide seems to answer your question:
Spring 为这个操作提供了一些有用的支持,参考指南似乎回答了你的问题:
There is not a standard single way to create an appropriate PreparedStatement (which explains why the method signature is the way it is). An example that works on Oracle and may not work on other platforms is...
没有一种标准的单一方法来创建适当的 PreparedStatement(这解释了为什么方法签名是这样的)。一个适用于 Oracle 但可能不适用于其他平台的示例是...
I've tested this example on MySQL and it works there too, but I can't speak for other platforms.
我已经在 MySQL 上测试了这个例子,它也可以在那里工作,但我不能说其他平台。
回答by Jeff Miller
For databases that conform to SQL-99, you can use identity columns: CREATE TABLE sometable (id INTEGER GENERATED ALWAYS AS IDENTITY(START WITH 101) PRIMARY KEY, ...
对于符合 SQL-99 的数据库,可以使用标识列:CREATE TABLE sometable (id INTEGER GENERATED ALWAYS AS IDENTITY(START WITH 101) PRIMARY KEY, ...
Use getGeneratedKeys()to retrieve the key that was just inserted with executeUpdate(String sql, int autoGeneratedKeys). Use Statement.RETURN_GENERATED_KEYS for 2nd parameter to executeUpdate()
使用getGeneratedKeys()检索刚刚通过executeUpdate(String sql, int autoGeneratedKeys)插入的密钥。使用 Statement.RETURN_GENERATED_KEYS 作为第二个参数来执行更新()
回答by atripathi
extraneon's answer, although correct, doesn't work for Oracle.
extraneon 的答案虽然正确,但不适用于 Oracle。
The way you do this for Oracle is:
您为 Oracle 执行此操作的方式是:
String key[] = {"ID"}; //put the name of the primary key column
ps = con.prepareStatement(insertQuery, key);
ps.executeUpdate();
rs = ps.getGeneratedKeys();
if (rs.next()) {
generatedKey = rs.getLong(1);
}
回答by ANURAG SHARMA
Just declare id column as id integer not NULL primary key auto_increment
只需将 id 列声明为 id 整数而不是 NULL 主键 auto_increment
after this execute this code
在此之后执行此代码
ResultSet ds=st.executeQuery("select * from user");
while(ds.next())
{
ds.last();
System.out.println("please note down your registration id which is "+ds.getInt("id"));
}
ds.close();
the above code will show you the current row's id
上面的代码将显示当前行的 id
if you remove ds.last()
than it will show all values of id column
如果您删除ds.last()
它,它将显示 id 列的所有值