//*************************************************************************** // (c) Copyright IBM Corp. 2007 All rights reserved. // // The following sample of source code ("Sample") is owned by International // Business Machines Corporation or one of its subsidiaries ("IBM") and is // copyrighted and licensed, not sold. You may use, copy, modify, and // distribute the Sample in any form without payment to IBM, for the purpose of // assisting you in the development of your applications. // // The Sample code is provided to you on an "AS IS" basis, without warranty of // any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR // IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Some jurisdictions do // not allow for the exclusion or limitation of implied warranties, so the above // limitations or exclusions may not apply to you. IBM shall not be liable for // any damages you suffer as a result of using, copying, modifying or // distributing the Sample, even if IBM has been advised of the possibility of // such damages. //*************************************************************************** // // SOURCE FILE NAME: UDFcli.sqlj // // SAMPLE: Call the UDFs in UDFsrv.java // // Parameter Style used in this program is "DB2GENERAL". // // Steps to run the sample with command line window: // I) If you have a compatible make/nmake program on your system, // do the following: // 1. Update makefile with a valid (userid, ) password and // an available port number. // 2. Compile the server source file UDFsrv.java (this will also // compile the Utility file, Util.sqlj, erase the existing // library/class files and copy the newly compiled class files, // UDFsrv.class and Person.class from the current directory // to the $(DB2PATH)\function directory): // nmake/make UDFsrv // 3. Compile the client source file UDFcli (this will also call // the script 'udfcat' to create and catalog the UDFs): // nmake/make UDFcli // 4. Run the client UDFcli: // java UDFcli // // II) If you don't have a compatible make/nmake program on your // system, do the following: // 1. Compile the server source file with the following command: // javac UDFsrv.java // 2. Erase the existing library/class files (if exists), // UDFsrv.class and Person.class from the following path, // $(DB2PATH)\function. // 3. copy the class files, UDFsrv.class and Person.class from // the current directory to the $(DB2PATH)\function. // 4. Register/catalog the UDFs with: // udfcat // 5. Compile the utility file with the following command: // sqlj Util.sqlj // 6. Update bldsqljs and bldsqlj build files with a valid userid // and password. // 7. Build the SQLj UDFs with with: // bldsqlj UDFcli // 8. Run UDFcli with: // java UDFcli // // SQL Statements USED: // FETCH // SELECT // // // Output will vary depending on the JDBC driver connectivity used. //*************************************************************************** // // For more information on the sample programs, see the README file. // // For information on developing SQLJ applications, see the Application // Development Guide. // // For information on using SQL statements, see the SQL Reference. // // For the latest information on programming, compiling, and running DB2 // applications, visit the DB2 Information Center at // http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/index.jsp //**************************************************************************/ import java.sql.*; // JDBC classes import sqlj.runtime.*; import sqlj.runtime.ref.*; #sql iterator CursorForScratchpadScUDF(int, String, String); #sql iterator CursorForScUDFReturningErr(String, String, double); #sql iterator CursorForTableUDF(String, String, double); class UDFcli { public static void main(String argv[]) { try { Db db = new Db(argv); System.out.println(); System.out.println("THIS SAMPLE SHOWS HOW TO WORK WITH UDFs."); // connect database db.getDefaultContext(); demoExternalScratchpadScalarUDF(); demoExternalScalarUDFReturningErr(); demoExternalTableUDF(); } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } } // main static void demoExternalScratchpadScalarUDF() { try { System.out.println(); System.out.println( "----------------------------------------------------------\n" + "USE THE SQL STATEMENTS:\n" + " SELECT\n" + "TO WORK WITH SCRATCHPAD SCALAR UDF."); // use SCRATCHPAD scalar UDF System.out.println(); System.out.println(" Use the SCRATCHPAD scalar UDF:\n" + " SELECT scratchpadScUDF(), name, job\n" + " FROM staff\n" + " WHERE name LIKE 'S%'"); CursorForScratchpadScUDF cur; int counter = 0; String name = null; String job = null; #sql cur = {SELECT scratchpadScUDF(), name, job FROM staff WHERE name LIKE 'S%'}; System.out.println(" COUNTER NAME JOB\n" + " ------- ---------- -------"); #sql {FETCH :cur INTO :counter, :name, :job}; while (!cur.endFetch()) { System.out.println(" " + Data.format(counter, 7) + " " + Data.format(name, 10) + " " + Data.format(job, 7)); #sql {FETCH :cur INTO :counter, :name, :job}; } cur.close(); } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } } // demoExternalScratchpadScalarUDF static void demoExternalScalarUDFReturningErr() { try { System.out.println(); System.out.println( "----------------------------------------------------------\n" + "USE THE SQL STATEMENTS:\n" + " SELECT\n" + "TO WORK WITH SCALAR UDF THAT RETURNS ERROR."); // use scalar UDF System.out.println(); System.out.println( " Use the scalar UDF that returns error:\n" + " SELECT name, job, scUDFReturningErr(salary, 0.00)\n"+ " FROM staff\n" + " WHERE name LIKE 'S%'"); CursorForScUDFReturningErr cur; String name = null; String job = null; double comm = 0.0; #sql cur = {SELECT name, job, scUDFReturningErr(salary, 0.00) FROM staff WHERE name LIKE 'S%'}; System.out.println(" NAME JOB COMM\n" + " ------- ---------- --------"); #sql {FETCH :cur INTO :name, :job, :comm}; while(!cur.endFetch()) { System.out.println(" " + Data.format(name, 7) + " " + Data.format(job, 10) + " " + Data.format(comm, 7, 2)); #sql {FETCH :cur INTO :name, :job, :comm}; } cur.close(); } catch (SQLException e) { if (e.getSQLState().equals("38999")) { System.out.println(); System.out.println("--------- Expected Error ---------\n"); } SqljException sqljExc = new SqljException(e); sqljExc.handle(); } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } } // demoExternalscalarUDFReturningErr static void demoExternalTableUDF() { try { System.out.println(); System.out.println( "----------------------------------------------------------\n" + "USE THE SQL STATEMENTS:\n" + " SELECT\n" + "TO WORK WITH TABLE UDF."); // use table UDF System.out.println(); System.out.println( " Use the table UDF:\n" + " SELECT udfTb.name, udfTb.job, udfTb.salary\n" + " FROM TABLE(TableUDF(1.5)) AS udfTb"); CursorForTableUDF cur; String name = null; String job = null; double newSalary = 0.0; #sql cur = {SELECT udfTb.name, udfTb.job, udfTb.salary FROM TABLE(TableUDF(1.5)) AS udfTb}; System.out.println(" NAME JOB SALARY\n" + " ------- ---------- --------"); #sql {FETCH :cur INTO :name, :job, :newSalary}; while (!cur.endFetch()) { System.out.println(" " + Data.format(name, 7) + " " + Data.format(job, 10) + " " + Data.format(newSalary, 7, 2)); #sql {FETCH :cur INTO :name, :job, :newSalary}; } cur.close(); } catch (Exception e) { SqljException sqljExc = new SqljException(e); sqljExc.handle(); } } // demoExternalTableUDF }