Primary key from inserted row jdbc?

亡梦爱人 提交于 2019-12-17 15:46:34

问题


Is there a cross database platform way to get the primary key of the record you have just inserted?

I noted that this answer says 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?

If not how would you suggest I implement this for a piece of code that could access any of SQL Server, MySQL and Oracle?


回答1:


Copied from my code:

pInsertOid = connection.prepareStatement(INSERT_OID_SQL, Statement.RETURN_GENERATED_KEYS);

where pInsertOid is a prepared statement.

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.




回答2:


extraneon's answer, although correct, doesn't work for Oracle.

The way you do this for Oracle is:

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);
}



回答3:


Have you tried the Statement.executeUpdate() and Statement.getGeneratedKeys() methods? There is a developerWorks article that mentions the approach.

Also, in JDBC 4.0 Sun added the row_id feature that 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.

Good luck!




回答4:


for oracle, Hibernate uses NEXT_VALUE from a sequence if you have mapped a sequence for PKEY value generation.

Not sure what it does for MySQL or MS SQL server




回答5:


Spring provides some useful support for this operation and the reference guide seems to answer your question:

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...

I've tested this example on MySQL and it works there too, but I can't speak for other platforms.




回答6:


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, ...

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()




回答7:


Just declare id column as id integer not NULL primary key 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

if you remove ds.last() than it will show all values of id column



来源:https://stackoverflow.com/questions/201887/primary-key-from-inserted-row-jdbc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!