Chapter 26. Using Java with PL/SQL

One of Oracle8i’s major new features is the ability to store and execute Java programs inside the database, giving Oracle developers another server-side development language (the first language, of course, is PL/SQL). These Java stored procedures (JSPs) open up a whole new realm of potential applications. For example, developers can use the Java networking classes to allow their database applications to communicate directly with network applications, such as a web server, on other computers.

Fortunately for us, Oracle has made PL/SQL and Java interoperable, allowing PL/SQL developers to call JSPs and Java developers to call PL/SQL stored procedures. This chapter tests your ability to define PL/SQL wrappers that allow you to call JSPs within PL/SQL.

Beginner

26-1.

Oracle8i’s Java Virtual Machine is called:

  1. SunSpot

  2. Nova

  3. Aurora

  4. Prometheus

  5. Zeus

26-2.

True or false?

  1. Java performance stinks because you have to download an applet across the network.

  2. You must know Java to take advantage of it in Oracle8i.

  3. You can call Java-stored procedures from a variety of clients, including SQL, PL/SQL, Java applets, Pro*C, Visual Basic, and Oracle Forms.

  4. Except for its ugly syntax, Java is just like PL/SQL.

  5. Java has hundreds of built-in classes to use in your PL/SQL programs.

  6. Java is a replacement for PL/SQL.

  7. Java, like PL/SQL, is a proprietary language controlled by Oracle.

  8. You can run your Java programs on a variety of platforms with very little effort.

  9. Java programs, such as C or C++ programs, are compiled into binary executables.

  10. The Java language is case-sensitive (i.e., the variable names junk, Junk, and JUNK are different.)

26-3.

Java programs that are compiled and stored in the database are called:

  1. Internal Java

  2. Java stored procedures

  3. Oracle Jclass files

  4. External procedures

  5. HotJava

26-4.

PL/SQL procedures and functions are most similar to Java:

  1. Classes

  2. Variables

  3. Enterprise JavaBeans

  4. Methods

  5. Objects

26-5.

What kind of file is created when you compile a Java program?

  1. .src

  2. .exe

  3. .obj

  4. .class

  5. .jvm

26-6.

Which of the following establishes a logical correspondence between a Java method and a PL/SQL procedure or function (standalone or packaged) or a member method of a SQL object type:

  1. Call specification (call spec)

  2. External definition

  3. Remote method invoker

  4. IDL definition

  5. CORBA object

26-7.

Consider the following Java program:

public class xxx {
   public static void doit () {
       System.out.println("Hello World!!!");
   }
}

The missing element (denoted by xxx) is the:

  1. Filename

  2. Class name

  3. Procedure name

  4. Method name

  5. Package name

26-8.

Consider the following Java program:

public class helloWorld {
   public static void xxx () {
       System.out.println("Hello World!!!");
   }
}

The missing element (denoted by xxx) is the:

  1. Filename

  2. Class name

  3. Procedure name

  4. Method name

  5. Package name

26-9.

Consider the following Java program:

public class simpleJava {
  public static void printIt (xxx s) {
     System.out.println (s);
  }
}

The missing element (denoted by xxx) is the:

  1. Parameter name

  2. Parameter datatype

  3. Call spec

  4. Object name

  5. Modifier

26-10.

Consider the following Java program:

public class simpleMath {

   public static xxx fact ( int N ) {
      int retVal = N;
      for (int i=(N-1); i > 0; i--) {
         retVal = retVal * i;
      }
      return retVal;
   }
}

The missing element (denoted by xxx) is the:

  1. Method parameter name

  2. Class datatype

  3. Call spec

  4. Object return type

  5. Method return type

26-11.

Suppose that you have compiled the following Java program and loaded it into the database:

public class simpleMath {

   public static xxx fact ( int N ) {
      int retVal = N;
      for (int i=(N-1); i > 0; i--) {
         retVal = retVal * i;
      }
      return retVal;
   }
}

Which of the following commands allows you to call the Java program from within PL/SQL to calculate a factorial?

  1. CREATE OR REPLACE FUNCTION factorial (n NUMBER) RETURN NUMBER
    AS LANGUAGE JAVA
    NAME 'fact.simpleMath(int) return int';
  2. retVal := CALL_JAVA(NAME ‘simpleMath.fact(‘ || N || ’) return int’);

  3. CREATE OR REPLACE PROCEDURE factorial (n NUMBER, f OUT NUMBER)
    AS JAVA CLASS
    NAME 'simpleMath.fact(int) return int';
  4. CREATE OR REPLACE FUNCTION factorial (n NUMBER) RETURN NUMBER
    AS LANGUAGE JAVA
    NAME 'simpleMath.fact(int) return int';
  5. CREATE OR REPLACE FUNCTION factorial (n NUMBER) RETURN NUMBER
    AS LANGUAGE JAVA
    NAME 'simpleMath.fact';

Intermediate

26-12.

Put the following steps for accessing a Java class from PL/SQL in order:

Grant the necessary privileges on the PL/SQL wrappers.
Use javac (or an Integrated Development Environment such as JDeveloper ) to compile the Java code.
Write PL/SQL wrappers to publish the class’ methods.
Call the PL/SQL wrapper programs.
Use loadjava to load the class into the database.

26-13.

Show the operating system commands that load the following class into the SCOTT (password TIGER) schema:

public class helloWorld {
   public static void doit () {
       System.out.println("Hello World!!!");
   }
}

26-14.

Show the Oracle8i DDL command that loads the following class into the database:

public class helloWorld {
   public static void doit () {
       System.out.println("Hello World!!!");
   }
}

26-15.

What clause must you include in a function or procedure header to create a call spec for a Java method?

  1. PARALLEL_ENABLE

  2. LANGUAGE JAVA

  3. JAVA SPEC

  4. AUTHID USER

  5. DETERMINISTIC

26-16.

What is the Java equivalent of the command SET SERVEROUTPUT ON?

26-17.

To publish a void Java method, you should use a PL/SQL:

  1. Function

  2. Blob

  3. Procedure

  4. Index-by table

  5. External procedure

26-18.

For each PL/SQL datatype in the left column, provide a corresponding Java datatype (there are many possible answers):

PL/SQL Datatype

Java Equivalent

VARCHAR2

 

DATE

 

NUMBER (integer)

 

NUMBER (real)

 

ROWID

 

OBJECT

 

RAW

 

26-19.

Write a call spec for the following Java class:

public class OutputTest {
  public static void printIt (String s) {
     System.out.println (s);
  }
}

26-20.

Write a call spec for the following Java method:

public class Fibonacci {
  public static int fib (int n) {
    if (n == 1 || n == 2)
      return 1;
    else
      return fib(n - 1) + fib(n - 2);
  }
}

26-21.

The following Java program is saved in a file called test.java. What happens when you try to use javac to compile it?

public class helloWorld {
   public static void doit () {
       System.out.println("Hello World!!!");
   }
}

Expert

26-22.

Describe what happens when the call spec specified for the following class is created and executed:

public class helloWorld {
   public static void doit () {
       System.out.println("Hello World!!!");
   }
}

-- Call spec used for the class
CREATE OR REPLACE PROCEDURE my_hello
AS LANGUAGE JAVA
NAME 'helloWorld.doIt()';

26-23.

Describe what happens when the call spec specified for the following class is created and executed:

public class helloWorld {
   public void doit () {
       System.out.println("Hello World!!!");
   }
}

-- Call spec used for the class
CREATE OR REPLACE PROCEDURE my_hello
AS LANGUAGE JAVA
NAME 'helloWorld.doit()';

26-24.

Write a query to display information about the Java objects in a user schema.

26-25.

Write a procedure to print the source code for a Java-stored procedure (assuming, of course, that the source has been loaded).

26-26.

Write a call spec for the following Java class:

public class NumberTest{
  public static void smallestFirst (int[] a, int[] b) {
    int tmp = a[0];
    if (a[0] > b[0]) {
       a[0] = b[0];
       b[0] = tmp;
    }
  }
}

26-27.

Complete the following call spec:

CREATE OR REPLACE FUNCTION wages (e Employee) RETURN NUMBER
AS LANGUAGE JAVA
NAME 'Paymaster.wages( xxx ) return BigDecimal';

26-28.

You have been asked to design a PL/SQL interface for a subset of the methods in a Java file-manipulation class. Specifically, you are to write two functions and one procedure: a function that deletes a given file and returns a Boolean flag indicating whether the delete succeeded; a function that returns a file’s size; and a procedure that uses an OUT parameter, based on a index-by table type, to return the names of all the files in a given directory. Base your procedure on the following Java class:

import java.io.File;

public class JFile {

   public static int tVal () { return 1; };  // True Value
   public static int fVal () { return 0; };  // False Value
   public static String listDelimiter () { return "|"; };  // File Delimiter

   // Return the length of the file
   public static long length (String fileName) {
      File myFile = new File (fileName);
      return myFile.length();
      }

   // Delete a file
   public static int delete (String fileName) {
      File myFile = new File (fileName);
      boolean retval = myFile.delete();
      if (retval) return tVal(); else return fVal();
      }

   // Return a list of file names delimited by "|"
   public static String dirContents (String dir) {
      File myDir = new File (dir);
      String[] filesList = myDir.list();
      String contents = new String();
      for (int i = 0; i < filesList.length; i++)
         contents = contents + listDelimiter() + filesList[i];
      return contents;
      }

   /*
   || Lots more classes...
   */
}

Write a specification for your package.

26-29.

The Jfile class defined in 26-28 uses three “get” methods—tval(), fval(), and listDelimiter( )—to represent (respectively) the Boolean TRUE value, the Boolean FALSE value, and the delimiter character used by the dirContents method. As a first step in implementing your file package, develop a method to retrieve and use these same values in your PL/SQL program.

26-30.

Implement the function to delete a file; remember that it must return a Boolean value.

26-31.

Implement the function to retrieve a file’s size.

26-32.

Implement the procedure to populate an index-by table with the contents of a directory.

26-33.

Before you start using the package, you have to set up the appropriate permissions. What new role do you need to grant to a schema to allow it to read or write a file? What new role do you need to create a file? What INIT.ORA parameter do you need to modify to control the directories in which you can perform file operations?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.44.223