Call an Oracle function from Java

后端 未结 4 623
鱼传尺愫
鱼传尺愫 2020-12-04 01:41

I am having issues calling an Oracle FUNCTION (not a Stored Procedure) from Java 1.6, using ojdbc14.jar.

I do not know what the function contains as I am calling it

相关标签:
4条回答
  • 2020-12-04 01:55

    it should be:

    String call = "{ ? = call FCRLIVE.AP_CH_GET_ACCT_BALANCES(?, ?, ?, ?, ?) }";
    
    0 讨论(0)
  • 2020-12-04 01:59

    You need to define parameter returned by function:

    String call = "{ ? = call FCRLIVE.AP_CH_GET_ACCT_BALANCES(?, ?, ?, ?, ?) }";
                      CallableStatement cstmt = conn.prepareCall(call);
                      cstmt.setQueryTimeout(1800);
                      cstmt.registerOutParameter(1, ...Type returned by function);
                      cstmt.setString(2, inputCode);
                      cstmt.registerOutParameter(3, oracle.jdbc.OracleTypes.NUMBER);
                      cstmt.registerOutParameter(4, oracle.jdbc.OracleTypes.VARCHAR);
                      cstmt.registerOutParameter(5, oracle.jdbc.OracleTypes.CHAR);
                      cstmt.registerOutParameter(6, oracle.jdbc.OracleTypes.CHAR);
                      cstmt.executeUpdate();
    
    0 讨论(0)
  • 2020-12-04 02:04

    your return parameter is only one. the first one. that's the only one you must register its type. so, first thing first:

    cstmt.registerOutParameter(1, oracle.jdbc.OracleTypes.VARCHAR);

    then set/register the other parameters as needed, but you have 6 parameters as in question marks and you handle only 5. you'll need to set the 6th one as well:

    cstmt.setString(6, myVariable);

    if it is not clear, please post the prototype of sql function you are using and I'll point you to exact missing binding.

    0 讨论(0)
  • 2020-12-04 02:11

    There are actually multiple ways of doing so. But the easiest of them all is firing a query. Here's how to do it.

    String sql="select myFunction('"+number+"','"+date"') from dual";
    statement.execute(sql);
    

    Set the input and output parameters if you are using JDBC.

    If you are using hibernate use Named Queries something like this: YourMapping.hbm.xml

    <sql-query name="my_function" callable="true">
    <return alias="demo" class="net.bean.Demo">
    <return-property name="id" column="id"/>
    <return-property name="fname" column="fname"/>
    <return-property name="lname" column="lname"/>
    </return>
        {?=call demoFunc(:param1,:param2)}
    </sql-query>
    

    Now this will create a Named Query for the function

    Next thing to do is simply call it using following code

    Query query=session.getNamedQuery("my_function");
    query.setParameter("parma1",date);
    query.setParameter("parma2",number);
    query.executeUpdate();
    

    Note that in hbm.xml file the return class name and properties exists only apply if you have mapped the returning values if the function returning appropriate values.

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