If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Need help in creating JAVA UDF

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-17-07, 08:27
azonin azonin is offline
Registered User
 
Join Date: Apr 2007
Posts: 2
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 passed to it (the previous), 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
Reply With Quote
  #2 (permalink)  
Old 08-17-07, 13:35
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
No need for the scratchpad. Just add a class member to store the last value.
Code:
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.)
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 08-18-07, 08:16
azonin azonin is offline
Registered User
 
Join Date: Apr 2007
Posts: 2
Hi Stolze,
now works fine. Thank you for your very quick and accurate reply.

Ciao und vielen danke !

Alessandro
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On