Calling SQL function from Java and setting parameters

548 Views Asked by At

I am trying to call a SQL Function from my Java code but I have no luck in this. I know I need to use Callable Statement to call the SQL function but I am not sure about the function what values it will yield as a result since I am new to SQL and I am having hard time understanding the SQL function. Below is my SQL function.

SQL function:

FUNCTION FXRATE_ENTITY(CODCURRBASE IN VARCHAR, LCODCURRFROM IN VARCHAR, LCODCURRTO IN VARCHAR, LIDENTITY IN VARCHAR)
  RETURN NUMBER IS
  CODCUR_L VARCHAR2(3);
BEGIN
  IF (LCODCURRTO = '*') THEN
    RETURN NVL(FXRATE(CODCURRBASE, LCODCURRFROM, CODCURRBASE, LIDENTITY),
               0);
  ELSE
    RETURN NVL(FXRATE(CODCURRBASE, LCODCURRFROM, LCODCURRTO, LIDENTITY), 0);
  END IF;
END;
1

There are 1 best solutions below

0
PKey On

You could try something like this:

public String callPlsql(String a,String b, String c) throws Exception {
                String res=null;
                CallableStatement cstmt = null;
                Connection con=null;
                try {
                con = getConn(); //define this to get your connection 
                cstmt = con.prepareCall("{? = call FXRATE_ENTITY(?,?,?)}");
                cstmt.registerOutParameter(1, java.sql.Types.NUMERIC);
                cstmt.setString(2, a);
                cstmt.setString(3, b);
                cstmt.setString(4, c);
                cstmt.execute();
                res = cstmt.getString(1);
                //con.commit();
                }
                catch(Exception e){
                   con.rollback();
                   e.printStackTrace();
                   throw e;
                }
                finally {
                  cstmt.close();
               }
  }