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:
SunSpot
Nova
Aurora
Prometheus
Zeus
26-2.
True or false?
Java performance stinks because you have to download an applet across the network.
You must know Java to take advantage of it in Oracle8i.
You can call Java-stored procedures from a variety of clients, including SQL, PL/SQL, Java applets, Pro*C, Visual Basic, and Oracle Forms.
Except for its ugly syntax, Java is just like PL/SQL.
Java has hundreds of built-in classes to use in your PL/SQL programs.
Java is a replacement for PL/SQL.
Java, like PL/SQL, is a proprietary language controlled by Oracle.
You can run your Java programs on a variety of platforms with very little effort.
Java programs, such as C or C++ programs, are compiled into binary executables.
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:
Internal Java
Java stored procedures
Oracle Jclass files
External procedures
HotJava
26-4.
PL/SQL procedures and functions are most similar to Java:
Classes
Variables
Enterprise JavaBeans
Methods
Objects
26-5.
What kind of file is created when you compile a Java program?
.src
.exe
.obj
.class
.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:
Call specification (call spec)
External definition
Remote method invoker
IDL definition
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:
Filename
Class name
Procedure name
Method name
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:
Filename
Class name
Procedure name
Method name
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:
Parameter name
Parameter datatype
Call spec
Object name
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:
Method parameter name
Class datatype
Call spec
Object return type
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?
CREATE OR REPLACE FUNCTION factorial (n NUMBER) RETURN NUMBER
AS LANGUAGE JAVA
NAME 'fact.simpleMath(int) return int';
retVal := CALL_JAVA(NAME ‘simpleMath.fact(‘ || N || ’) return int’);
CREATE OR REPLACE PROCEDURE factorial (n NUMBER, f OUT NUMBER)
AS JAVA CLASS
NAME 'simpleMath.fact(int) return int';
CREATE OR REPLACE FUNCTION factorial (n NUMBER) RETURN NUMBER
AS LANGUAGE JAVA
NAME 'simpleMath.fact(int) return int';
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?
PARALLEL_ENABLE
LANGUAGE JAVA
JAVA SPEC
AUTHID USER
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:
Function
Blob
Procedure
Index-by table
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?