簡體   English   中英

在Java中,Oracle PL / SQL中的調用過程或函數。 返回結果集false

[英]In Java calling procedure or function in Oracle PL/SQL. Return Resultset false

我有一個函數plsql,請在plsql中運行函數,返回游標有數據,但是我在java問題中將此函數稱為返回游標為空,請幫我處理此問題

這是函數oracle plsql

FUNCTION get_canh_bao
      ( p_toolname      in  varchar2,
        p_toolid        in  varchar2,
        p_toolurl       in  varchar2,
        p_parent        in  varchar2)
      RETURN  sys_refcursor IS

    s           varchar2(32000);
    ptype       varchar2(32000);
    pday        varchar2(32000);
    psql        varchar2(32000);
    pcreate     varchar2(32000);
    re          sys_refcursor;
    pwhere      varchar2(32000);
BEGIN
--
    pwhere := '';
    s := 'select b.TYPE_REPORT, b.DAY_NHAPLIEU, a.sql_cmd, a.name_createdate'
        ||' from cpcdata.tbl_config_nhaplieu a'
        ||' left join cpcdata.tbl_mainmenu b'
        ||' on a.tool_id = b.ID '
        ||' where b.show_all = 0 and b.flag = 1 and b.ID = '''||p_toolid||'''';
    execute immediate s INTO ptype, pday, psql, pcreate;
            -- Tinh ngay canh bao
            if (INSTR(psql,'where') > 0 ) then
                pwhere := ' and ';
            else
                pwhere := ' where ';
            end if;
            CASE
                WHEN ptype = 'day'    THEN
                    s := psql ||pwhere|| ' to_char('||pcreate||',''dd/mm/yyyy'') = to_char(sysdate - '||pday||',''dd/mm/yyyy'')';
                WHEN ptype = 'week'   THEN
                    s := psql ||pwhere||pcreate||' between to_date(TRUNC (sysdate, ''iw''),''dd/mm/yyyy'') and'
                              || ' to_date(TRUNC(sysdate, ''iw'') + 7 - 1/86400- '||pday||',''dd/mm/yyyy'')';
                WHEN ptype = 'month'  THEN
                    s := psql ||pwhere||pcreate||' between to_date(TRUNC (sysdate, ''mm''),''dd/mm/yyyy'') and'
                              || ' to_date(LAST_DAY(TRUNC (sysdate, ''mm'')) + 1 - 1/86400 - '||pday||',''dd/mm/yyyy'')';
                WHEN ptype = 'quy'    THEN
                    s := psql ||pwhere||pcreate||' between to_date(TRUNC(sysdate, ''Q''),''dd/mm/yyyy'') and'
                              || ' to_date(add_months(trunc(sysdate,''Q''),3)- 1 - '||pday||', ''dd/mm/yyyy'')';
                WHEN ptype = 'year'   THEN
                    s := psql ||pwhere||pcreate||' between to_date(TRUNC (SYSDATE , ''YEAR''),''dd/mm/yyyy'') and'
                              || ' to_date(ADD_MONTHS(TRUNC (SYSDATE,''YEAR''),12) - 1 - '||pday||', ''dd/mm/yyyy'')';
                ELSE return null;
            END CASE;
    dbms_output.put_line(''||s);
    open re for s;
    RETURN re ;
    exception when others then
        declare
            s_err       varchar2(2000);
            str         varchar(2000);
            c_err       sys_refcursor;
        begin
            s_err := 'loi khi lay du lieu '|| sqlerrm;
            str := 'select '||s_err||' from dual';
            open c_err for str;
            return c_err;
        end;
END;

這是代碼java調用函數

ArrayList arr = new ArrayList();
            sql = "begin ? := config_pkg.get_canh_bao(?,?,?,?); end;";
            cs = conn.prepareCall(sql);
            cs.registerOutParameter(1, OracleTypes.CURSOR);
            cs.setString(2, "");
            cs.setString(3, "502");
            cs.setString(4, "");
            cs.setString(5, "");
            cs.executeQuery();
            ResultSet rs = (ResultSet)cs.getObject(1);
            System.out.println("------------Exception---------------"+rs.next());
            while (rs.next()) {

              System.out.println("a");
              String[] str =
              {"1" };
              arr.add(str);
            }

您應該使用綁定變量,類似於以下內容:

pwhere := '';
s := 'select b.TYPE_REPORT, b.DAY_NHAPLIEU, a.sql_cmd, a.name_createdate'
    ||' from cpcdata.tbl_config_nhaplieu a'
    ||' left join cpcdata.tbl_mainmenu b'
    ||' on a.tool_id = b.ID '
    ||' where b.show_all = 0 and b.flag = 1 and b.ID = :toolId';
execute immediate s INTO ptype, pday, psql, pcreate USING toolId;
        -- Tinh ngay canh bao
        if (INSTR(psql,'where') > 0 ) then
            pwhere := ' and ';
        else
            pwhere := ' where ';
        end if;
        s := psql ||pwhere||pcreate||' between :aDate AND :bDate';

        CASE
            WHEN ptype = 'day'    THEN ...
            WHEN ptype = 'week'   THEN
                open re for s USING TRUNC (sysdate, 'iw'), TRUNC(sysdate, 'iw') + 7 - 1/86400- pday;
            WHEN ptype = 'month'  THEN ...
        END CASE;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM