Java Stored Procedure

Date: 22/May/2003

Server side tracing in Java Stored Procedure

Introduction

Stored procedures are Java methods published to SQL and stored in an Oracle database for general use. A stored procedure can accept arguments, reference Java classes, and return Java result values when called from a client application. Any output produced by the Java class during the execution is not displayed in the user screen. This is because the default output device on the database server is not the user screen but a trace file and hence System.out and System.err print to the current trace files.
This how to illustrates where to look for the trace files as well as how to redirect the output to the user screen when the Java Stored Procedure is executed within a PL/SQL block.

Pre-requisites for running the example

You will need the following to run this example -

  • Oracle9i Database, which can be downloaded from here .

Description

Often we use print statements in most of the java programs for debugging purposes. These statements print the messages to the standard output devise like the console. In the server the default output device is not the user screen instead a trace file. As a result all the System.out and System.err statements used in the Java Stored Procedure, print to the current trace file.


rset = stmt.executeQuery("SELECT ENAME FROM EMP");
  System.out.println("Employee names in the EMP table ...");
while ( rset.next() ) {
  System.out.println(rset.getString("ENAME"));
}


You can find the output statements in the latest trace file <TRACEFILE_NAME>.trc, located at <ORACLE_HOME>/admin/<SID>/udump

Where,
<ORA_HOME>            : The folder where oracle database is installed
<SID>                           : The SID used to connect to the database
<TRACEFILE_NAME> : The most recent trace file used by the database for logging

Now if you want to To redirect output to the SQL*Plus text buffer, call the procedure set_output() in package DBMS_JAVA, as follows:


SQL> SET SERVEROUTPUT ON

SQL> CALL dbms_java.set_output(2000);

 

The minimum (and default) buffer size is 2,000 bytes; the maximum size is 1,000,000 bytes.
Output is printed when the stored procedure exits.

Running the Sample

The complete source code for this sample can be viewed online. This section discusses the instruction to run the sample application

Step 1 Copy the Tracing.java to your working directory. Open this file in your editor, change the following line to your database connection parameters in the getEmployee() method ,
Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:@<hostname>:<port>:<SID>", "scott","tiger");

Step 2 Set CLASSPATH environment variable to include Oracle9i JDBC Driver file: classes12.zip or classes12.jar or ojdbc14.jar. Also ensure that PATH system variable includes the bin directory of JDK.

Step 3 Loading Java classes to the database
Run the loadjava command at the command prompt

loadjava -u scott/tiger@<DBHOST>:<PORT>:<SID> -verbose -resolve -thin <FILE_NAME>.java where,
<DBHOST>       : Name of the machine where the database is running
<PORT>            : The database port
<SID>                 : The SID used to connect to the databse
<FILE_NAME>   : The java stored procedure source file.

Example:
D:\Tracing\> loadjava -u scott/tiger@localhost:1521:ORCL -v -r -t Tracing.java

Step 4 Publishing the Java classes

CREATE OR REPLACE PROCEDURE getEmployees
AS LANGUAGE JAVA
NAME ’Tracing.getEmployees()’;

Step 5

Calling the stored procedure, from SQL Plus

BEGIN
getEmployees;
END;


Check the output in the console or in the trace file. It may look like,

Employee names in the EMP table ...

ALLEN


WARD


RAM


MARTIN

Resources


E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy