简体   繁体   中英

How to call oracle function from java

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

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.

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);

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(?, ?, ?, ?, ?) }"; 

You can use CallableStatement

String sql="begin ? := aaaa.fucntion(?,?); 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)

// 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM