Calling oracle function from Java JDBC -


we have oracle function trying call jdbc. takes 3 inputs (string, number , date) , returns 1 number:

create or replace function     mww_avgcost (in_prd_lvl_number prdmstee.prd_lvl_number%type, in_org_lvl_number orgmstee.org_lvl_number%type, in_sales_date     prcmstee.prc_from_date%type) return number begin    

using java jdbc code follows:

        string call = "{ ? = call pmm.mww_avgcost(?, ?, ?) }";         callablestatement cstmt = conn.preparecall(call);         cstmt.registeroutparameter(1, java.sql.types.integer);         cstmt.setstring(2, productnumber);         cstmt.setint(3, storenumber);          // convert xml input sql date         java.sql.date sqldate = new java.sql.date(saledate.togregoriancalendar().gettimeinmillis());          cstmt.setdate(4, sqldate);         cstmt.execute();         bigdecimal resultfromfunction = cstmt.getbigdecimal(1);         log.info("resultfromfunction:" + resultfromfunction); 

the result returns 1 though , not proper number. have run fine sql developer same parameters , looks fine. proper way call sql function jdbc?

looks have type mismatch. you're setting out parameter sql integer getting bigdecimal.
if sql function returns integer use: callablestatement.getint(1)

if sql function returns floating point number, use callablestatement.registeroutparameter(1, java.sql.types.double) , callablestatement.getdouble(1)

the mapping java , oracle types can found here: https://docs.oracle.com/javase/1.5.0/docs/guide/jdbc/getstart/mapping.html

if need bigdecimal because of rounding problems or something, sql function should return decimal or numeric. , should set out parameter , getbigdecimal() you're doing now.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -