我试图在Java中使用可调用语句调用Postgres函数。该函数的一个输入参数是Timestamp类型的。当我尝试从java调用该函数时,我得到以下错误:
ERROR: function insrt_data(character varying, integer, bigint, unknown, character varying) does not exist
我的函数签名是:
insrt_data(
i_cd character varying,
i_prog_id smallint,
i_stat_id bigint,
i_dt date,
i_stat_cd character varying) RETURNS character varying
我的jdbc调用如下。StrtDt是java.util.Date字段。
String sql = "{call mySchema.insrt_data(?, ?, ?, ?, ?)}";
java.sql.Date StartDt = new java.sql.Date(record.getStrtDt().getTime());
Map<String, Object> map = jdbcTemplate.call(connection -> {
CallableStatement statement = connection.prepareCall(sql);
statement.setString(1, record.getCd());
statement.setInt(2, record.getProgId());
statement.setLong(3, record.getId());
statement.setDate(4, StartDt);
statement.setString(5, record.getStatCd());
statement.registerOutParameter(1, Types.VARCHAR);
return statement;
}, paramList);
问题出在i_prog_cd
。当我传递int
时,函数期望smallint
作为值。Postgres的smallint
对应于java中的short
。将其更改为short
后,代码工作了!