I am using NamedParameterJdbcTemplate for run pl/sql script. But I don't know how can I get the values of out variables (:id_out). Thanks in advance.
String script = "declare
begin
if myFunc(:id_in) is null then
:id_out := 0;
else
:id_out := 1;
end if;
end;";
Map<String,Object> bindVars = new HashMap<String, Object>();
bindVars.put(id_in,1);
bindVars.put(id_out,2);
jdbcTmpl.execute(script, bindVars, new PreparedStatementCallback<Object>() {
@Override public Object doInPreparedStatement(PreparedStatement cs)
throws SQLException, DataAccessException {
cs.execute();
return null;
}
}
);
I don't believe you can use a NamedParameterJdbcTemplate (or any other subclass of JdbcTemplate) with anonymous PL/SQL blocks such as that above. You'll have to wrap your anonymous PL/SQL block into a stored procedure or function.
Spring is intended to be portable across databases. As far as I know, neither MySQL nor SQL Server have a concept analogous to Oracle's anonymous PL/SQL blocks (I'm happy to be proved wrong on this point, though). Since this feature isn't portable across databases, Spring can't really support it for just Oracle.
It can be done with a plain JdbcTemplate as in the following example, but note the way that the OUT value must be specified in the anonymous plsql block with "? := 'something';
java parameters used below: String id="12345" and String fixSql=
Note the two question marks above - the first is effectively an IN parameter and the second an OUT parameter. This code will call it: