dbTalk Databases Forums  

Help on Java UDF writing to external file

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss Help on Java UDF writing to external file in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
John Hopfield
 
Posts: n/a

Default Help on Java UDF writing to external file - 11-02-2010 , 08:27 AM






Hi all,

Its possible to create a java UDF that write some text into an
external file?
DB2 v.9.72
On WindowsXP pro

I want the possibility to write in sql


select MyDebugFunction('C:\test.txt','hello world')
from SYSIBM.sysdummy1

JH

Reply With Quote
  #2  
Old   
AD
 
Posts: n/a

Default Re: Help on Java UDF writing to external file - 11-08-2010 , 05:16 PM






Create UdfZ.java

import java.io.*;

public class UdfZ {

public static String MyDebugFunction (String plik, String tresc){
String ret = "OK";
try
{
BufferedWriter out = new BufferedWriter(new FileWriter(plik));
out.write(tresc);
out.close();
}
catch (IOException e) {
ret = e.getMessage();
}

return ret;
}

public static String MyDebugFunctionAppend (String plik, String
tresc){
String ret = "OK";
try
{
BufferedWriter out = new BufferedWriter(new
FileWriter(plik,true));
out.write(tresc);
out.close();
}
catch (IOException e) {
ret = e.getMessage();
}

return ret;
}

}


compile it and copy class file into $DB2/sqllib/function directory
then call sql

CREATE FUNCTION MyDebugFunctionAppend(VARCHAR(100),VARCHAR(100))
RETURNS VARCHAR(100)
EXTERNAL NAME 'UdfZ!MyDebugFunctionAppend'
LANGUAGE JAVA
PARAMETER STYLE JAVA
DETERMINISTIC
FENCED
CALLED ON NULL INPUT
NO SQL
NO EXTERNAL ACTION

CREATE FUNCTION MyDebugFunctionAppend(VARCHAR(100),VARCHAR(100))
RETURNS VARCHAR(100)
EXTERNAL NAME 'UdfZ!MyDebugFunctionAppend'
LANGUAGE JAVA
PARAMETER STYLE JAVA
DETERMINISTIC
FENCED
CALLED ON NULL INPUT
NO SQL
NO EXTERNAL ACTION


select MyDebugFunctionAppend('d:\out.txt','test1') from
sysibm.sysdummy1

Reply With Quote
  #3  
Old   
John Hopfield
 
Posts: n/a

Default Re: Help on Java UDF writing to external file - 11-11-2010 , 11:22 AM



On 9 Nov, 00:16, AD <ada... (AT) gmail (DOT) com> wrote:
Quote:
Create UdfZ.java

..
..
select MyDebugFunctionAppend('d:\out.txt','test1') from
sysibm.sysdummy1
Thank you AD!
It works.

The only problem i have is that out.newLine() doesn't work.
Also out.write(s+"\r\n") doesn't work...

I don't know why.

JH

Reply With Quote
  #4  
Old   
AD
 
Posts: n/a

Default Re: Help on Java UDF writing to external file - 11-11-2010 , 04:09 PM



On 11 Lis, 18:22, John Hopfield <Hopfi... (AT) freemail (DOT) it> wrote:
Quote:
On 9 Nov, 00:16, AD <ada... (AT) gmail (DOT) com> wrote:

Create UdfZ.java

..
..
select MyDebugFunctionAppend('d:\out.txt','test1') from
sysibm.sysdummy1

Thank you AD!
It works.

The only problem i have is that out.newLine() doesn't work.
Also out.write(s+"\r\n") doesn't work...

I don't know why.

JH
Replace

out.write(tresc);
out.close();

with

out.write(tresc);
out.newLine();
out.close();

it works for me

Reply With Quote
  #5  
Old   
AD
 
Posts: n/a

Default Re: Help on Java UDF writing to external file - 11-11-2010 , 04:17 PM



On 11 Lis, 23:09, AD <ada... (AT) gmail (DOT) com> wrote:
Quote:
On 11 Lis, 18:22, John Hopfield <Hopfi... (AT) freemail (DOT) it> wrote:









On 9 Nov, 00:16, AD <ada... (AT) gmail (DOT) com> wrote:

Create UdfZ.java

..
..
select MyDebugFunctionAppend('d:\out.txt','test1') from
sysibm.sysdummy1

Thank you AD!
It works.

The only problem i have is that out.newLine() doesn't work.
Also out.write(s+"\r\n") doesn't work...

I don't know why.

JH

Replace

out.write(tresc);
out.close();

with

out.write(tresc);
out.newLine();
out.close();

it works for me
Also check this link
http://www.ibm.com/developerworks/da...0302izuha.html

Reply With Quote
  #6  
Old   
John Hopfield
 
Posts: n/a

Default Re: Help on Java UDF writing to external file - 11-12-2010 , 02:29 AM



On 11 Nov, 23:09, AD <ada... (AT) gmail (DOT) com> wrote:
Quote:
out.write(tresc);
out.newLine();
out.close();

it works for me
i have added out.newLine() and then recompiled the java class and re-
installed into "FUNCTION" folder.
But...don't works
After a reboot now is OK.

Thank you
JH

Reply With Quote
  #7  
Old   
AD
 
Posts: n/a

Default Re: Help on Java UDF writing to external file - 11-12-2010 , 04:04 AM



On 12 Lis, 09:29, John Hopfield <Hopfi... (AT) freemail (DOT) it> wrote:
Quote:
On 11 Nov, 23:09, AD <ada... (AT) gmail (DOT) com> wrote:

out.write(tresc);
out.newLine();
out.close();

it works for me

i have added out.newLine() and then recompiled the java class and re-
installed into "FUNCTION" folder.
But...don't works
After a reboot now is OK.

Thank you
JH

After recompiling and copying refreshed class you should call

db2 "CALL SQLJ.REFRESH_CLASSES()"

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.