调用定义类型的函数

时间:2014-07-28 22:31:29

标签: function postgresql jdbc types

我创建了自己的Postgres功能:

CREATE OR REPLACE FUNCTION myfunction(
param_val1 varchar(255),
param_cal2 VARCHAR(255), 
param_val3 VARCHAR(255),
param_val4 current_category,
param_val5 current_type,
param_val6  VARCHAR(255),
param_val7 bigint,
param_val8 text )

我想用jdbc调用它:

callableStatement = conn.prepareCall("{call myfunction(?,?,?,?,?,?,?,?)}");

  callableStatement.setString(1, "item1");
  callableStatement.setString(2, "domain1");
  callableStatement.setString(3, "indicator1");
  callableStatement.setString(4, "PROPERTY");
  callableStatement.setString(5, "STRING");
  callableStatement.setString(6, "on");
  callableStatement.setLong(7, 1396983600);
  callableStatement.setString(8, "testing");

但我明白了:

org.postgresql.util.PSQLException: ERROR: function addindicators(character varying,     character varying, character varying, character varying, character varying, character varying, bigint, character varying) does not exist
  Hint: No function matches the given name and argument types. You might need to add     explicit type casts.
  Position: 15

current_categorycurrent_type是枚举。我是否需要创建java枚举,然后使用setObject(#,Object)?

1 个答案:

答案 0 :(得分:0)

一种方法是将函数定义为采用text / varchar参数并转换为函数内的enum类型。示例代码:

CREATE OR REPLACE FUNCTION myfunction( ..., param_val4 text, ...)
...
DECLARE
  _param_val4 current_category := param_val4::current_category;
  ...
BEGIN
...

但是Java中可能有一种方法可以在没有明确类型的情况下调用此函数(我不知道)。如果您可以为枚举发送无类型字符串(类型为"unknown"的字符串文字),Postgres会自动找到该函数的最佳匹配。