Call an Oracle function from Java

后端 未结 4 625
鱼传尺愫
鱼传尺愫 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 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

    
    
    
    
    
    
        {?=call demoFunc(:param1,:param2)}
    
    

    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.

提交回复
热议问题