有没有办法从一个准备的语句检索自动增量ID

有没有办法从一个数据库查询检索自动生成的密钥时使用Java语句与准备语句。

例如,我知道AutoGeneratedKeys可以工作如下。

stmt = conn.createStatement(); stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); if(returnLastInsertId) { ResultSet rs = stmt.getGeneratedKeys(); rs.next(); auto_id = rs.getInt(1); } 

然而。 如果我想用一个准备好的语句做一个插入操作。

 String sql = "INSERT INTO table (column1, column2) values(?, ?)"; stmt = conn.prepareStatement(sql); //this is an error stmt.executeUpdate(Statement.RETURN_GENERATED_KEYS); if(returnLastInsertId) { //this is an error since the above is an error ResultSet rs = stmt.getGeneratedKeys(); rs.next(); auto_id = rs.getInt(1); } 

有没有办法做到这一点,我不知道。 从javadoc似乎PreparedStatements不能返回自动生成的ID。

是。 看到这里 。 第7.1.9节。 将您的代码更改为:

 String sql = "INSERT INTO table (column1, column2) values(?, ?)"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.executeUpdate(); if(returnLastInsertId) { ResultSet rs = stmt.getGeneratedKeys(); rs.next(); auto_id = rs.getInt(1); } 

有几种方法,似乎不同的jdbc驱动程序处理的东西有点不同,或根本没有在某些情况下(有些只会给你自动生成的主键,而不是其他列),但基本的forms是

 stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); 

或者使用这种forms:

 String autogenColumns[] = {"column1","column2"}; stmt = conn.prepareStatement(sql, autogenColumns) 

是的,有一个方法。 我只是发现这隐藏在Java文档。

他们的方式是传递AutoGeneratedKeys id如下

 String sql = "INSERT INTO table (column1, column2) values(?, ?)"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); 

我是那些通过几个线程冲浪寻找解决这个问题的人之一,并最终得到它的工作。 用于这些用法jdbc:oracle:thin:with ojdbc6.jar请注意:您可以使用以下任一方法:(方法1)

 Try{ String yourSQL="insert into Table1(Id,Col2,Col3) values(SEQ.nextval,?,?)"; myPrepStatement = <Connection>.prepareStatement(yourSQL, Statement.RETURN_GENERATED_KEYS); myPrepStatement.setInt(1, 123); myPrepStatement.setInt(2, 123); myPrepStatement.executeUpdate(); ResultSet rs = getGeneratedKeys; if(rs.next()) { java.sql.RowId rid=rs.getRowId(1); //what you get is only a RowId ref, try make use of it anyway U could think of System.out.println(rid); } } catch (SQLException e) { // } 

(方法2)

 Try{ String yourSQL="insert into Table1(Id,Col2,Col3) values(SEQ.nextval,?,?)"; //IMPORTANT: here's where other threads don tell U, you need to list ALL cols //mentioned in your query in the array myPrepStatement = <Connection>.prepareStatement(yourSQL, new String[]{"Id","Col2","Col3"}); myPrepStatement.setInt(1, 123); myPrepStatement.setInt(2, 123); myPrepStatement.executeUpdate(); ResultSet rs = getGeneratedKeys; if(rs.next()) { //In this exp, the autoKey val is in 1st col int id=rs.getLong(1); //now this's a real value of col Id System.out.println(id); } } catch (SQLException e) { // } 

基本上,如果你只是想要SEQ.Nextval的值,那么就不要使用Method1,只要返回RowID就可以破解你的头,find使用它的方法,这也适合你所有的数据types它来! 这在MySQL,DB2中可能正常工作(返回实际的val),但不在Oracle中。

并且,在debugging时closures你的SQL Developer,Toad或者使用相同login会话的任何客户端来执行INSERT。 它可能不会影响你每次(debugging调用)…直到你发现你的应用程序冻结一段时间毫无例外。 是的…无一例外地停下来!

  Connection connection=null; int generatedkey=0; PreparedStatement pstmt=connection.prepareStatement("Your insert query"); ResultSet rs=pstmt.getGeneratedKeys(); if (rs.next()) { generatedkey=rs.getInt(1); System.out.println("Auto Generated Primary Key " + generatedkey); }