Retrieve anonymous PLSQL block result

前端 未结 2 1468
伪装坚强ぢ
伪装坚强ぢ 2021-01-26 14:45

I have some trouble retrieving the result of an anonymous PLSQL block in java.

Here is the block :

DECLARE
in_cnt_date DATE := \'&1\';
hv_cnt_id NUM         


        
相关标签:
2条回答
  • 2021-01-26 15:23

    It is because the implicit date conversion is failing. Add TO_DATE() instead of directly assigning the date string to a date variable. If java.sql.Date is used, the TO_DATE() is not required.

    The implicit conversion usually depends on the session's NLS_DATE_FORMAT.

    In your case in_cnt__date DATE := '&1' is the culprit. &1 will actually be attempted to convert into a date.. And hence the exception thrown!

    public static final String CONTEXT = "DECLARE in_cnt__date DATE := ? ;" +
    "hv_cnt_id NUMBER := 0; " +
    "BEGIN DBMS_OUTPUT.ENABLE (NULL); " +
    "INSERT INTO dt_contexts (CNT_ID, CNT_CONTEXT, CNT_TYPE, CNT_SOURCE, CNT_COMMENT, CNT_DATE, CNT_DATE_INSERT, CNT_DATE_UPDATE) " +
    "VALUES (0, 'EPE_CONTEXT', 'ROUTE', 'bdd', 'Built from ROUTE', in_cnt__date, SYSDATE, SYSDATE); " +
    "SELECT SEQ_DT_CNT_ID.CURRVAL INTO hv_cnt_id FROM DUAL; " +
    "? := hv_cnt_id;
    "EXCEPTION WHEN OTHERS THEN RAISE ; END;";
    

    And then,

    cs.setDate(1, (java.sql.Date) Route.datePrf);
    

    Will set the date for in_cnt__date;

    Finally, to retreive the values in hv_cnt_id The below is added to your PL/SQL block

    "? := hv_cnt_id;"
    

    And from JDBC, we get it like,

     cs.setDate(1, (java.sql.Date) Route.datePrf);
     cs.registerOutParameter(2, Types.NUMBER);
     cs.execute();
     contextId = cs.getInt(2);
    
    0 讨论(0)
  • 2021-01-26 15:27

    Remove all exception handlers from the PL/SQL code, and execute the block and you will see the exact line number where the error occurs.

    Raise inside exception will never let you know the exact error details. Please read http://lalitkumarb.wordpress.com/2014/05/02/when-others-then-null-a-bug/ Focus on the RAISE part.

    Once you do it, you will find that there is an data type conversion issue.

    0 讨论(0)
提交回复
热议问题