Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2008-06-22
Medium Priority
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);
COM.ibm.db2.jdbc.DB2Exception: [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
  • 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?
COM.ibm.db2.jdbc.DB2Exception: [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 COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:275)
        at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:217)
        at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(SQLExceptionGenerator.java:449)
        at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute2(DB2PreparedStatement.java:2195)
        at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute(DB2PreparedStatement.java:3967)
        at ...UDFTester.main(UDFTester.java:39)
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 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