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 > Java Stored Procedure with OUT parameter

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-16-11, 13:21
marc_ marc_ is offline
Registered User
 
Join Date: Aug 2011
Location: Glasgow, UK
Posts: 36
Post Java Stored Procedure with OUT parameter

Hi,

I'm trying to execute a Java stored proc that verifies whether a file exists or not but I'm having trouble with the OUT parameter when I try to call it from CLP:
db2 => call testFileExists()
SQL4306N Java stored procedure or user-defined function
"DB2INST1.FILE_EXISTS", specific name "SQL110816190528900" could not call Java
method "cHECKFILE", signature "(Ljava/lang/String;[". SQLSTATE=42724

However, if I change the DB2 wrapper stored proc to "IN" on the second parameter it runs! Albeit not properally working because it need to be an out parameter.

Are there any specific rules for OUT parameters concerning Java SPs?

Any ideas would be fantastic

Marc
-- DB2 9.7.2 on a Linux VM (Ubuntu Server 10.04)


CREATE PROCEDURE FILE_EXISTS (IN input CHAR(200), OUT isExists INT)
DYNAMIC RESULT SETS 0
DETERMINISTIC
LANGUAGE JAVA
PARAMETER STYLE JAVA
NO SQL
FENCED
THREADSAFE
PROGRAM TYPE SUB
EXTERNAL NAME 'FILE_EXISTS!cHECKFILE'


//FILE_EXISTS.java
import java.io.*;

public class FILE_EXISTS
{
public static void cHECKFILE (String input, int isExists) throws Exception
{
String errorMessage;

try
{
// Check file exists and pass the value back thru the OUT paramenter

File f = new File(input);

if (f.isFile()) isExists = 1;
else
isExists = 0;

}
catch (SecurityException ioe)
{
errorMessage = ioe.getMessage();
throw new IOException( errorMessage + " FAILED" );
}
}
}



-- testFileExistsSP.sql
CREATE PROCEDURE testFileExists()
BEGIN
DECLARE ISEXISTS INT DEFAULT 0;

CALL FILE_EXISTS ('/home/db2inst1/test_file.txt',ISEXISTS);

IF ISEXISTS = 1 THEN
CALL runSHCommand('echo exists > /home/db2inst1/testFileExists.txt');
ELSE
CALL runSHCommand('echo notexists > /home/db2inst1/testFileExists.txt');
END IF;
END
@
Reply With Quote
  #2 (permalink)  
Old 08-16-11, 14:12
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Check examples in the manual. The out parameter must be declared as an array in Java.
Reply With Quote
  #3 (permalink)  
Old 08-16-11, 17:41
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
The reason for that is that Java uses call-by-value semantics on parameters - unless you have an array, in which case call-by-reference is applied. But if the method shall return some values, call-by-value wouldn't work, obviously. That's why the array is needed so that DB2 will actually see the modification made inside the method to the passed-in parameter.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 08-16-11, 19:14
marc_ marc_ is offline
Registered User
 
Join Date: Aug 2011
Location: Glasgow, UK
Posts: 36
Thanks for the replies!

So if I understand this correctly I should use:
public static void cHECKFILE (String input, int[] isExists) throws Exception
in the method call and
isExists[0] = 1;

Setting the 0th element of the of the int array which will cause the value to be passed by ref back to the DB2 calling proc?

Funny thing is I get the OUT param always equals zero when passed back to the calling stored proc no matter what I set it to in the Java prog.

Is there something else I need to do?
Reply With Quote
  #5 (permalink)  
Old 08-17-11, 05:32
marc_ marc_ is offline
Registered User
 
Join Date: Aug 2011
Location: Glasgow, UK
Posts: 36
Cool

Ignore me! That will teach me to code late at night

I've done a fresh test and it works fine.

Thanks for all the help -- this is a great forum

Reply With Quote
  #6 (permalink)  
Old 08-17-11, 05:57
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
I just wanted to tell you to have a look at sample sqllib/samples/java/SpServer.java, method "outParameter". :-)
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 08-17-11, 06:27
marc_ marc_ is offline
Registered User
 
Join Date: Aug 2011
Location: Glasgow, UK
Posts: 36
Cool

That's a good example - thanks for the tip.

I'll test this out -- as it looks like it's got examples of VARCHAR -> String mappings (which I was having trouble with earlier).

btw loved your article: Making Operating System Calls from SQL

Last edited by marc_; 08-17-11 at 06:29. Reason: typo
Reply With Quote
  #8 (permalink)  
Old 08-17-11, 08:08
marc_ marc_ is offline
Registered User
 
Join Date: Aug 2011
Location: Glasgow, UK
Posts: 36
Are java stored procs cached somewhere?

It seems that :
1. Copying the class file to ~sqllib/function/
2. DROPping and recreating the corresponding wrapper Strored Proc.
Is not enough ..


I seem to find that DB2 is hanging on to a previous param list as it's failing at runtime. However, if I merely rename the offending class and the wrapper SP it works fine.

Last edited by marc_; 08-17-11 at 08:08. Reason: typo
Reply With Quote
  #9 (permalink)  
Old 08-17-11, 08:26
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
IBM DB2 9.5 Information Center for Linux, UNIX, and Windows

Use NO for dev/test environments.

IIRC, there used to be command to 'refresh' the environment for java routines. I'll try to find if one is available in 9.5
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #10 (permalink)  
Old 08-17-11, 08:30
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #11 (permalink)  
Old 08-17-11, 08:39
marc_ marc_ is offline
Registered User
 
Join Date: Aug 2011
Location: Glasgow, UK
Posts: 36
Thanks!

I thought that:
CALL SQLJ.REFRESH_CLASSES()

ONLY related to SQLJ routines rather than JDBC Java SPs.

The things you learn *here*

Last edited by marc_; 08-17-11 at 08:51. Reason: typo
Reply With Quote
Reply

Tags
db2, java, stored procedures

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