没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。职位:15 :: Callable Statement Spring

时间:2015-08-06 09:26:56

标签: java spring postgresql

jdbcTemplate.execute(new CallableStatementCreator() {

                @Override
                public CallableStatement createCallableStatement(final Connection paramConnection)
                        throws SQLException {
                    final CallableStatement cs = paramConnection.prepareCall("{call INSERT_ACP_ASSIGN_FUNCTION(?,?,?)}");
                    cs.setString(1, resourceId); 
                    cs.setInt(2,resourceType); 
                    cs.setArray(3, new PostgreSQLArray(accessControlPolicyIds));
                    return cs;
                }
            }, new CallableStatementCallback() {
                public Object doInCallableStatement(final CallableStatement cs) throws SQLException{
                    cs.execute();
                    return null; 
                }
            });

public class PostgreSQLArray实现了Array {

private final String[] stringArray;
private final String stringValue;

public PostgreSQLArray(String[] stringArray) {
    this.stringArray = stringArray;
    this.stringValue = stringArrayToPostgreSQLArrayString(stringArray);
}

public String toString() {
    return stringValue;
}

/**
 * 
 * @param inputtedArray source String array
 * @return string representation of a given integer array
 */
    public static String stringArrayToPostgreSQLArrayString(String[] inputtedArray) {
        if ( inputtedArray == null ) {
            return "NULL";
        }
        final int al = inputtedArray.length;
        if ( al == 0 ) {
            return "{}";
        }
        StringBuilder sb = new StringBuilder(); 
        sb.append('{');
        for (int i = 0; i < al; i++) {
            if ( i > 0 ) sb.append(',');
            sb.append(inputtedArray[i]);
        }
        sb.append('}');
        return sb.toString();
    }


    public Object getArray() throws SQLException {
        return stringArray == null ? null : Arrays.copyOf(stringArray, stringArray.length);
    }

    public Object getArray(Map<String, Class<?>> map) throws SQLException {
        return getArray();
    }

    public Object getArray(long index, int count) throws SQLException {
        return stringArray == null ? null : Arrays.copyOfRange(stringArray, (int)index, (int)index + count );
    }

    public Object getArray(long index, int count, Map<String, Class<?>> map) throws SQLException {
        return getArray(index, count);
    }

    public int getBaseType() throws SQLException {
        return java.sql.Types.INTEGER;
    }

    public String getBaseTypeName() throws SQLException {
        return "varchar";
    }

    public ResultSet getResultSet() throws SQLException {
        throw new UnsupportedOperationException();
    }

    public ResultSet getResultSet(Map<String, Class<?>> map) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public ResultSet getResultSet(long index, int count) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public ResultSet getResultSet(long index, int count, Map<String, Class<?>> map) throws SQLException {
        throw new UnsupportedOperationException();
    }

    public void free() throws SQLException {
    }

}

CREATE OR REPLACE FUNCTION iot1_0."INSERT_ACP_ASSIGN_FUNCTION"(
    "I_RESOURCE_ID" character varying,
    "I_RESOURCE_TYPE" integer,
    "I_ACP_RESOURCE_ID" character varying[])
  RETURNS void AS
$BODY$ DECLARE
   acp_resource_id character varying;
   acp_id integer;
  BEGIN
    for acp_resource_id in select $3 LOOP
       RAISE NOTICE 'acp_resource_id is %', acp_resource_id;
    select ID into acp_id from iot1_0.ACP_DEFINE where RESOURCEID = acp_resource_id;

    RAISE NOTICE 'acp_id is %', acp_id;
        insert into iot1_0.ACP_ASSIGN(resource_type, acp_id, resource_id) values ($2 , acp_id, $1);
    END LOOP; 
  END;$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION iot1_0."INSERT_ACP_ASSIGN_FUNCTION"(character varying, integer, character varying[])
  OWNER TO enterprisedb;

我曾经在Postgres管理员中执行的SQL是

SELECT iot1_0."INSERT_ACP_ASSIGN_FUNCTION"(
    character varying '112w',
     1,
    array['1','2','3']
);

如果我执行thsi函数,它正在执行正常但是当我从java代码调用它时抛出异常:

  

引起:org.postgresql.util.PSQLException:错误:函数   insert_acp_assign_function(字符变化,整数,字符   vary [])不存在提示:没有函数匹配给定的名称   和参数类型。您可能需要添加显式类型转换   位置:15点   org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2270)     在   org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1998)     在   org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)     在   org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:570)     在   org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:420)     在   org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:413)     在   org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:169)

请帮助我对postgres非常新。我好多天都在挣扎。

1 个答案:

答案 0 :(得分:0)

您是否尝试过添加“iot1_0”。打电话?您是否在PgAdmin中从同一用户运行它?您可能还想从Java代码中运行queries listing available procedures之一 - 以查看哪些方法应该可见。