Discussion:
Need help in creating JAVA UDF
(too old to reply)
a***@it.ibm.com
2007-08-17 12:46:23 UTC
Permalink
Need help in creating JAVA UDF

Hi all,
I'm newbie on java programming.

I'm trying to develop a JAVA UDF that returns the n-1 value (the previous) passed to it, I think that using SCRATCHPAD is a good idea.

Here is my java code for a UDF that simply returns the integer value you pass it:

CREATE FUNCTION testIt(INTEGER) RETURNS INTEGER
EXTERNAL NAME 'ACFTestFunc!testIt'
not fenced
language java
parameter style db2general
no sql
scratchpad
no external action

import java.sql.*;
class ACFTestFunc extends COM.ibm.db2.app.UDF
{
public void testIt(int inValue, int result)
throws Exception
{
set(2,inValue);
}
}

Someone colud help me to modify this code ?

Thanks a lot
Ciao

Alessandro
a***@it.ibm.com
2007-08-18 12:22:35 UTC
Permalink
Reply from a german collegue :

No need for the scratchpad. Just add a class member to store the last value.

class ACFTestFunc extends COM.ibm.db2.app.UDF
{
int lastValue;
public void testIt(int inValue, int result) throws Exception
{
set(2, lastValue);
lastValue = inValue;
}
}


You may want to use the FINAL CALL clause in the function declaration so that you can initialize "lastValue" to something on the first invocation of the UDF.

Also note that the object will be destroyed at the end of the SQL statement execution. So you can carry values between one call to the function to the next call - within the same SQL statement. (It may work across SQL statements because DB2 does some caching, but it is unreliable at best.)

Fielen Danke Stolz !!!!

:-)

Loading...