我似乎无法获得正确的魔法组合来完成这项工作:
OracleDataSource ods = new oracle.jdbc.pool.OracleDataSource(); ods.setURL("jdbc:oracle:thin:app_user/pass@server:1521:sid"); DefaultContext conn = ods.getConnection(); CallableStatement st = conn.prepareCall("INSERT INTO tableA (some_id) VALUES (1) RETURNING ROWID INTO :rowid0"); st.registerReturnParameter(1, OracleTypes.ROWID); st.execute();
我得到的错误是"违反协议".如果我更改为registerOutParameter(),我会收到通知我没有注册所有返回变量.如果我将语句包装在PL/SQL中开始; 结束; 然后我使用常规registerOutParameter()调用得到参数.我真的更愿意避免在PL/SQL中包装所有的insert语句 - 那么上面缺少什么?
通常,您不希望依赖于代码数据库.您应该使用CallableStatement而不是OraclePreparedStatement.
CallableStatement statement = connection.prepareCall("{call INSERT INTO tableA (some_id) VALUES (1) RETURNING ROWID INTO ? }"); statement.registerOutParameter( 1, Types.VARCHAR ); int updateCount = statement.executeUpdate(); if (updateCount > 0) { return statement.getString(1); }
你需要做的一些事情
将CallableStatement更改为OracleCallableStatement
尝试并返回NUMBER,即:OracleTypes.Number
从查询返回信息的示例代码:
OraclePreparedStatement pstmt = (OraclePreparedStatement)conn.prepareStatement( "delete from tab1 where age < ? returning name into ?"); pstmt.setInt(1,18); /** register returned parameter * in this case the maximum size of name is 100 chars */ pstmt.registerReturnParameter(2, OracleTypes.VARCHAR, 100); // process the DML returning statement count = pstmt.executeUpdate(); if (count>0) { ResultSet rset = pstmt.getReturnResultSet(); //rest is not null and not empty while(rset.next()) { String name = rset.getString(1); ... } }
有关Oracle JDBC扩展的更多信息:
http://download-uk.oracle.com/docs/cd/B19306_01/java.102/b14355/oraint.htm