从Java源代码调用Postgres过程不起作用

问题描述

我正在使用Postgres 12,并尝试从Java源代码调用存储过程。当我试图从类中的一个简单函数调用过程时,它可以正常工作并提供输出,但是当我试图在运行WebLogic的应用程序中调用该过程时,却无法正常工作。

public int test(String i_oid,String i_msisdn,String i_cardtype,String i_content,Double i_clientid,String i_smscid) {
    int o_rc = -1;
        
    try {
        stmt = connection.prepareCall("call pkg_sms$enqueue_sm(?,?,?)");
        stmt.registerOutParameter(9,java.sql.Types.DOUBLE);
        stmt.setDouble(1,i_clientid);
        stmt.setString(2,i_oid);
        stmt.setString(3,i_msisdn);
        stmt.setString(4,"");
        stmt.setString(5,i_cardtype);
        stmt.setString(6,"");
        stmt.setString(7,i_content);
        stmt.setString(8,i_smscid);
        stmt.execute();

        o_rc = (int) stmt.getDouble(9);

        if (o_rc == 0) {
            log.log_sys(Level.INFO,"SMS to MSISDN:[" + i_msisdn + "] is tested,cliendId:["+i_clientid.toString()+"]");
        } else {
            log.log_sys(Level.INFO,"Fail to test SMS: RC:[" + o_rc + "] MSISDN:[" + i_msisdn + "],cliendId:["+i_clientid.toString()+"]");
        }
    } catch(sqlRecoverableException sqlre) {    
        log.log_sys(Level.ERROR,"#DB_Execution:Calling test1() sqlRecoverableException" + sqlre);
        log.log_sys(Level.WARN,"#DB_Execution:DB Connection Closed = ["+ disconnectDB()+"]");
    } catch(sqlException e) {   
        log.log_sys(Level.ERROR,"#DB_Execution:Calling test() Exception" + e);
        log.log_sys(Level.WARN,"#DB_Execution:DB Connection Closed = ["+ disconnectDB()+"]");
    } finally {
        try {
            if (stmt != null) stmt.close();
        } catch (Exception e2) {
            log.log_sys(Level.ERROR,"#DB_Execution:Statement Close Exception" + e2);
        }
    }
    return o_rc;
}

存储过程:

CREATE OR REPLACE PROCEDURE pkg_sms$enqueue_sm(
    i_clientid double precision,i_oid text,i_ms text,i_serv_type text,i_card_type text,i_sub_type text,i_content text,i_smsc_id text,INOUT o_rc double precision DEFAULT NULL::double precision)
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
    INSERT INTO cnp.tbl_sm_queue (seqnum,clientid,oid,ms,serv_type,card_type,sub_type,content,status,smsc_id,create_on)
    VALUES (nextval('seq'),i_clientid,i_smsc_id,i_ms,i_serv_type,i_card_type,i_sub_type,i_content,'I',current_timestamp);
    o_rc := 0;
   
    EXCEPTION
        WHEN others THEN
        get stacked diagnostics
        o_rc   = returned_sqlstate;
            o_rc := - 1 * ABS(o_rc);
           
END;
$BODY$;

我得到的错误

org.postgresql.util.PsqlException:此语句未声明OUT参数。使用{?= call ...}声明一个

解决方法

该错误提示您在使用OUT参数调用存储过程时使用以下语法:

stmt = connection.prepareCall("{?= call pkg_sms$enqueue_sm(?,?,?)}");