Solved

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

Posted on 2008-06-22
5
956 Views
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":

  http://publib.boulder.ibm.com/infocenter/db2luw/v8/topic/com.ibm.db2.udb.doc/core/rsql0400.htm

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:

   http://publib.boulder.ibm.com/infocenter/db2luw/v8/topic/com.ibm.db2.udb.doc/ad/t0007049.htm

thanks in advance for your replies.





db2level:
 

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

    RETURNS INTEGER

    SPECIFIC suncertified.get_next_trans_id

------------------------------------------------------------------------

-- SQL UDF (Scalar)

------------------------------------------------------------------------

F1: BEGIN ATOMIC

   DECLARE aVar INTEGER;

    set aVar = nextval for iau_transaction_seq;

   RETURN aVar;

END

 

================

 

   db2 =>select suncertified.get_next_trans_id() from sysibm.sysdummy1 

   

   1

   -----------

              12

 

   1 record(s) selected.

================

 

 

...

Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");

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

func.execute();

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

0
Comment
Question by:wboyd
  • 3
  • 2
5 Comments
 
LVL 37

Expert Comment

by:momi_sabag
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
0
 
LVL 2

Author Comment

by:wboyd
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%

...;C:\DEVELO~1\IBM\SQLLIB\BIN;C:\DEVELO~1\IBM\SQLLIB\FUNCTION;C:\DEVELO~1\IBM\SQLLIB\SAMPLES\REPL;... 

Open in new window

0
 
LVL 37

Expert Comment

by:momi_sabag
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
0
 
LVL 2

Author Comment

by:wboyd
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.
0
 
LVL 2

Accepted Solution

by:
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");

result.next();

int xid = result.getInt(1); 

...

Open in new window

0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
What is ISQL? 6 101
MS Access 2003 Query 2 37
Access Database - Manually changing xid value 7 44
Data center mess 4 48
I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now