Oracle's RETURNING INTO usage in Java (JDBC, Prepared Statement)

前端 未结 2 644
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 05:14

I\'m using JDBC to execute Oracle statement which looks like this:

\"INSERT INTO MYTABLE(MYDATA) VALUES(?) RETURNING MY_CALCULATED_DATA INTO ?\"
// MYTABLE\'         


        
相关标签:
2条回答
  • 2020-12-03 05:48

    To get auto generated key we have method getGeneratedKeys method in preparestatement which return resultset that contain key value all we need is pass key column name to preparestatement

    pstm = con.prepareStatement("insert query",new String[]{primarykeycolumnname});
    int i = pstm.executeUpdate();
    if (i > 0) 
    {
        ResultSet rs = pstm.getGeneratedKeys();
        while(rs.next())
        {
            System.out.println(rs.getString(1)); 
        }
    }
    
    0 讨论(0)
  • 2020-12-03 05:59

    Because parameters specified in returning clauses are handled in a different way compared to normal output parameters(getReturnResultSet vs getResultSet vs returning parameters in a callablestatement).
    They need to be handled with OraclePreparedStatement. In the second case when you wrap the insert statement in begin..end the insert is handled by the database itself and al jdbc sees is an anonymous plsql block.
    http://docs.oracle.com/cd/E11882_01/java.112/e16548/oraint.htm#BABJJDDA

    0 讨论(0)
提交回复
热议问题