// Source File Name: UDFsrv.java 1.4
//
// Licensed Materials -- Property of IBM
//
// (c) Copyright International Business Machines Corporation, 1999.
// All Rights Reserved.
//
// US Government Users Restricted Rights -
// Use, duplication or disclosure restricted by
// GSA ADP Schedule Contract with IBM Corp.
// Sample Program UDFsrv - Java User-defined Functions
// Steps to run the sample:
// (1) create and populate the SAMPLE database (db2sampl)
// (2) (n)make UDFsrv
// (3) (n)make UDFcli
// (4) run UDFcli
// NOTES: (1) The jdk11_path database manager configuration parameter must
// be set
// (2) The CLASSPATH and shared library path environment variables
// must be set, as for any JDBC application.
// (3) Visit http://www.software.ibm.com/data/db2/java
// for current DB2 Java information
// For more information about this sample, refer to the README file.
// For more information on Programming in Java, refer to the
// "Programming in Java" section of the Application Development Guide.
// For more information on creating and using user-defined functions,
// refer to the "Object-Relational Programming" section of the
// Application Development Guide.
// For more information on building and running Java programs for DB2,
// refer to the "Building Java Applets and Applications" section of the
// Application Building Guide.
// For more information on the SQL language, refer to the SQL Reference.
// Class UDFsrv contains two methods:
// (1) scalarUDF
// (2) tableUDF
import java.lang.*; // for String class
import COM.ibm.db2.app.*; // UDF and associated classes
// the class Person is used by the table UDF
class Person
{ String name;
String job;
Person(){name = null; job =null;};
Person(String n , String j){name = n; job =j;};
public String getName(){ return name;};
public String getJob(){ return job;};
}
// /////
// Java user-defined functions are in this class
// /////
class UDFsrv extends UDF
{ // the scalar UDF
public void
scalarUDF(String inputString,
int outputStrLen) throws Exception
{ try
{ set(2, inputString.length());
}
catch (Exception e)
{ throw e;
}
}
// variables for the table UDF
String prefix; // just to use an input parameter
int row;
int maxRows;
Person[] smallStuff;
// the table UDF
public void
tableUDF (String inPrefix,
String outName,
String outJob) throws Exception
{ switch (getCallType())
{ case SQLUDF_TF_FIRST:
// do initialization which is independent of input parameters
break;
case SQLUDF_TF_OPEN:
// do initialization valid for this return table
prefix = inPrefix + "_";
smallStuff = new Person[4];
smallStuff[0] = new Person("Sanders" , "Mgr");
smallStuff[1] = new Person("Pernal" , "Sales");
smallStuff[2] = new Person("Marenghi" , "Mgr");
row = 1;
maxRows = 3;
break;
case SQLUDF_TF_FETCH:
if (row > maxRows)
{ // Set end-of-file signal and return
setSQLstate ("02000");
}
else
{ // Set the current output row and increment the row number
if (needToSet (2)) set (2, prefix + smallStuff[row - 1].getName());
if (needToSet (3)) set (3, prefix + smallStuff[row - 1].getJob());
row++;
}
break;
case SQLUDF_TF_CLOSE:
break;
case SQLUDF_TF_FINAL:
break;
}
}
}