how can i call a simple sql pl-defined function using jdbc?

Posted on 2008-06-22
Last Modified: 2008-06-30
i created - in the db2 development center -  a simple scalar sql pl-based udf that returns the next sequence number generated by a  sequence object (see code snippet).

the function runs perfectly in the db2 development center. it also runs perfectly from the db2 command line processor. so, i'd expect it to run with the attached code from a java app (see code snippet).

i'm doing the exact same thing as above in an oracle db (except for the db2-specific stuff, of course), and it works perfectly. but with db2, the code results in the exception attached (see code snippet).

i've been wrestling with this for the past 24 hours! i've googled my brains out searching for a solution. i've found this page describing "Reason code: "4".  SQLSTATE=42724":

it's just a simple function! there ain't no shared libraries or dlls involved! how come i can't call it from java like i can with an analogous function defined in an oracle db? please, don't tell me that i have to go through all the rigamarole here describing what's done in the examples in $DB2/SQLLIB/samples/java/jdbc:

thanks in advance for your replies.

DB21085I  Instance "DB2" uses "32" bits and DB2 code release "SQL08023" with level identifier "03040106".
Informational tokens are "DB2 v8.1.10.812", "s050811", "WR21362", and FixPak "10".
CREATE FUNCTION suncertified.get_next_trans_id(  )
    SPECIFIC suncertified.get_next_trans_id
-- SQL UDF (Scalar)
    set aVar = nextval for iau_transaction_seq;
   RETURN aVar;
   db2 =>select suncertified.get_next_trans_id() from sysibm.sysdummy1 
   1 record(s) selected.
Connection conn = DriverManager.getConnection("jdbc:db2:FOO","suncertified, "********");
CallableStatement func = conn.prepareCall(" {? = call suncertified.get_next_trans_id()} ");
func.registerOutParameter(1, java.sql.Types.INTEGER);
int xid = func.getInt(1);
================ [IBM][CLI Driver][DB2/NT] SQL0444N  Routine "*trans_id" (specific name "SQL080622121524410") is implemented with code in library or path "\get_next_trans_id", function "get_next_trans_id" which cannot be accessed.  Reason code: "4".  SQLSTATE=42724

Open in new window

Question by:wboyd
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 37

Expert Comment

ID: 21841033
when you write a function in sql/pl, db2 translate that function into C code and compiles it
that is why you receive the library path message
make sure that your PATH envrionment variable (if you use windows / linux) or your LIBPATH variable (if you use unix) is set up correctly when you run your java program

if you want to test it you could open a db2 command windows (you just run db2cmd.exe on windows->start) and try to run your java code from there, the variables should be set up correctly inside the console window

Author Comment

ID: 21841195
thanks for your reply, momi_sabag,

i did "Start -> Run -> db2cmd.exe"; a windows command shell appeared titled "DB2 CLP". at the command prompt, i added the java class that's trying to call the sql pl udf to the classpath. and then i ran the java class. i got the exact same exception i got hundreds of times in the last 24 hours (see code snippet).

please, can you spell out what you mean by "set up correctly"? in other words, what is a "correct" setup? $DB2HOME/SQLLIB/FUNCTIONS (among other things) is in my PATH. what else needs to be set up?

again, i created the udf in db2's development center. i used the DC's default project directory ("C:/Documents and Settings/%user_dir%/Application Data/IBM/DB2/DC/Projects/..."). i added that project dir to the PATH too - with no luck.

thanks again for trying to help. can anybody think of anything else, please?
... [IBM][CLI Driver][DB2/NT] SQL0444N  Routine "*TRANS_ID" (specific name "SQL080622165108270") is implemented with code in library or path "...NS_ID", function "SUNCERTIFIED.GET_NEXT_TRANS_ID" which cannot be accessed.  Reason co
de: "4".  SQLSTATE=42724
        at ...UDFTester.main(
got: 0
echo %PATH%

Open in new window

LVL 37

Expert Comment

ID: 21841208
when i say - set up correctly i mean
check what the path variable contains when you run your clp session which you say works fine with the above function
then, copy the value from the path variable from there to the path variable in the console window in which you run your java application

Author Comment

ID: 21841440
while in the 1st clp command window in which i mentioned that i successfully ran the udf, i typed "quit" to end the clp session. then - while in that same window - i typed "echo %PATH%". and it reported that the PATH values were exactly the same as the values in the 2nd command window that i launched with "Start -> run -> db2cmd.exe".

as soon as it finishes downloading, i plan to apply fixpack #11 to see if that makes any difference.

Accepted Solution

wboyd earned 0 total points
ID: 21841965
it turns out that sql pl udfs are NOT callable statements  (like sprocs are). with db2, sql pl-defined functions can only be used as part of an sql statement. who knew?

so instead of "{? = call suncertified.get_next_trans_id }", now i do a select like i did from the clp (see code snippet)

live and learn! ;¬)
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery("select get_next_trans_id() from sysibm.sysdummy1");;
int xid = result.getInt(1); 

Open in new window


Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question