3

How to call oracle function from java?.

I have a oracle function aaa.fucntion(number,date);, this method returns true or false. how do call this from java and get the returned value?

I am using Hibernate

this is what i tried,

Session session = null;
            String associateHistorySQL="";

            try {
                session = HibernateUtil.currentSession();

                associateHistorySQL = "SELECT aa.myFunction(:aorId,:givenDate) from dual";

                Query associateHistoryQuery = session.createQuery(associateHistorySQL);

                associateHistoryQuery.setParameter("aorId", associateOfficeRecordId);
                associateHistoryQuery.setParameter("givenDate", date);

                List associateHistoryList = associateHistoryQuery.list();

                if (associateHistoryList != null && associateHistoryList.size() > 0 && new Integer(associateHistoryQuery.uniqueResult().toString()) > 0)
                    return true;
                else
                    return false;
            } finally {
                HibernateUtil.cleanUpHibernateFromDao(false);
            }

This is the exception i get unexpected token: aa: line 1:1: unexpected token: aa

thanks

8
  • 1
    A SQL query? What have you tried, and where are you stuck? Commented Jul 23, 2014 at 5:55
  • hi @ErwinBolwidt, started server, will post the stacktrace in few minutes. Commented Jul 23, 2014 at 5:57
  • @ErwinBolwidt, could you look at the edit now?? Commented Jul 23, 2014 at 6:04
  • Better, but we need to know what the error/exception is that you're getting. None of us can run this, since we don't have your database (which is understandable in this situation) but that means that we can't tell where this code goes wrong. Commented Jul 23, 2014 at 6:06
  • @ErwinBolwidt updated with exception i get, thanks Commented Jul 23, 2014 at 6:10

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

Comments

4

Use session.doWork from hibernate.

How to call a Oracle function from hibernate with return parameter?

From Oracle documentation -

http://docs.oracle.com/cd/F49540_01/DOC/java.815/a64686/04_call5.htm

FUNCTION balance (acct_id NUMBER) RETURN NUMBER IS
  acct_bal NUMBER;
BEGIN
  SELECT bal INTO acct_bal FROM accts
    WHERE acct_no = acct_id;
  RETURN acct_bal;
END;

From a JDBC program, your call to the function balance might look like this:

CallableStatement cstmt = conn.prepareCall("{? = CALL balance(?)}");
cstmt.registerOutParameter(1, Types.FLOAT);
cstmt.setInt(2, acctNo);
cstmt.executeUpdate();
float acctBal = cstmt.getFloat(1);

Comments

2

oracle function:

FUNCTION ap_ch_get_acct_balances (VAR_PI_MOB_NO_ACCT_NO VARCHAR2,
VAR_REPLY_CODE OUT NUMBER, VAR_EXT_RESPONSE OUT VARCHAR2, VAR_PO_ACC_BAL OUT CHAR,
VAR_PO_ACCT_NO OUT CHAR)  

call in java:

String call = "{ ? = call FCRLIVE.AP_CH_GET_ACCT_BALANCES(?, ?, ?, ?, ?) }"; 

Comments

1

You can use CallableStatement

String sql="begin ? := aaaa.function(?,?); end;";
CallableStatement stmt = connection.prepareCall(sql);
stmt.registerOutParameter(1, OracleTypes.BOOLEAN);
stmt.setInt(2, number);
stmt.setTimestamp(3, date);
stmt.execute();

After that you can read the returned value with:

stmt.getBoolean(1)

Comments

0

// Oracle Function

create or replace function addtwono(a in number, b in number) return varchar2 is
Result varchar2(10);
begin
result:= a + b;
--c:= result;
return('SUCCESS');
end ;

// Java Code ==========================================
String query = "begin ? := user.addtwono(?,?); end;";
CallableStatement st = connection.prepareCall(query);
st.registerOutParameter(1, OracleTypes.VARCHAR);
st.setInt(2, 10);
st.setInt(3, 15);
st.execute();
String rtn = st.getString(1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.