java.sql.SQLException:参数类型冲突

时间:2016-09-02 09:34:22

标签: java oracle oracle12c

使用oracle 12c。这是我保存或更新的程序,在保存新记录的情况下应该返回id:

  procedure save_dsc (p_id  in out integer,
                 p_caption_name   in     varchar2,
                 p_dat_id         in     integer,
                 p_dsc_id         in out   integer)
is
begin
   if p_dsc_id is null
        then
            select c.id
              into p_dsc_id
              from pod_data_set_category c
             where c.dat_id = p_dat_id and dsc_id is null;
   end if;

    if p_id is null
    then
          insert into pod_data_set_category (id,
                                                 caption_name,
                                                 dat_id,
                                                 dsc_id)
        values (pod_dsc_seq.nextval,
                p_caption_name,
                p_dat_id,
                p_dsc_id) 
  returning id
          into p_id;
    else
        update pod_data_set_category
           set caption_name = p_caption_name,
               dsc_id = p_dsc_id
         where id = p_id;
    end if;
end;    

这是java代码:

  try (CallableStatement stmt = connection.prepareCall("{call save_dsc(?,?,?,?)}")) {
        stmt.registerOutParameter(1, Types.INTEGER);

        if (entryCategoryBean.getId() != null) {
            log.debug("Update");
            stmt.setInt(1, entryCategoryBean.getId());
            stmt.setNull(3, Types.NULL);
        } else {
            log.debug("Create");
            stmt.setNull(1, Types.INTEGER);
            stmt.setInt(3, entryCategoryBean.getDatasetId());
        }
        stmt.setString(2, entryCategoryBean.getName());
        if (entryCategoryBean.getParentId()!= null) {
            stmt.setInt(4, entryCategoryBean.getParentId());
        }
        else {
            stmt.setNull(4,Types.NULL);
        }
        stmt.registerOutParameter(4, Types.INTEGER);
        stmt.execute();

        if (entryCategoryBean.getId() == null) {
            entryCategoryBean.setId(stmt.getInt(1));
        }
    }

要么我保存或更新我得到java.sql.SQLException:参数类型冲突。 如何解决?

以这种方式固定:

procedure save_dsc (p_id  in out integer,
                 p_caption_name   in     varchar2,
                 p_dat_id         in     integer,
                 p_dsc_id         in    integer)
is
  v_dsc_id integer :=p_dsc_id;
begin
   if (p_dsc_id is null)
        then
            select c.id
              into v_dsc_id
              from pod_data_set_category c
             where c.dat_id = p_dat_id and dsc_id is null;

   end if;

    if p_id is null
    then
          insert into pod_data_set_category (id,
                                                 caption_name,
                                                 dat_id,
                                                 dsc_id)
        values (pod_dsc_seq.nextval,
                p_caption_name,
                p_dat_id,
                v_dsc_id) 
  returning id
          into p_id;
    else
        update pod_data_set_category
           set caption_name = p_caption_name,
               dsc_id = v_dsc_id
         where id = p_id;
    end if;
end;    

1 个答案:

答案 0 :(得分:0)

也许我完全错了,但您可能会尝试使用局部变量而不是直接使用输入/输出参数。

...
IS
  v_dsc_id INTEGER;
BEGIN
  IF (p_dsc_id IS NULL) THEN
    SELECT c.id
      INTO v_dsc_id
      ....;
    p_dsc_id := v_dsc_id;
  END IF;
  ...

当然,p_id参数

也是如此