Oracle DBMS_JAVA
Version 12.1.0.1

General Information
Library Note Morgan's Library Page Footer
The Library is currently in the process of being upgraded from Oracle Database Version 11.2.0.3 to 12.1.0.1. Demos are being upgraded to reflect the new Container paradigm as well as EBR (Edition Based Redefinition) and may contain references to CDBs, PDBs, and other objects you may not be familiar with such as CDB_OBJECTS_AE: Welcome to 12c.
Purpose Provides a PL/SQL interface for accessing database functionality from Java.

Because this package contains 90 functions and procedures thi spage is not complete. Only the active subprogram links, below, have been documented here.
AUTHID CURRENT_USER
Dependencies
ALL_JAVA_COMPILER_OPTIONS DBMS_JAVA_DEFINERS GET_ERROR$
DBA_JAVA_COMPILER_OPTIONS DBMS_JAVA_MISC JAVAVM_SYS
DBA_OBJECTS DBMS_JAVA_TEST PLITBLM
DBJ_SHORT_NAME DBMS_JVM_EXP_PERMS SDO_ROUTER_PARTITION
DBMS_ASSERT DBMS_OUTPUT USER_JAVA_COMPILER_OPTIONS
Documented Yes
Exceptions
Error Code Reason
ORA-29532 Untrapped Java Exception
First Available At or before 10gR1
Security Model Owned by SYS with EXECUTE granted to PUBLIC
Source {ORACLE_HOME}/rdbms/admin/initdbj.sql
Subprograms
COMPILE_CLASS FULL_NCOMP_ENABLED REVOKE_PERMISSION
COMPILE_METHOD GETVERSION RUNJAVA
DBMS_FEATURE_OJVM GET_COMPILER_OPTION RUNJAVA_IN_CURRENT_SESSION
DBMS_FEATURE_SYSTEM_OJVM GET_JDK_VERSION SEND_COMMAND
DECODE_NATIVE_COMPILER_OPTION GET_OJVM_PROPERTY SET_COMPILER_OPTION
DELETE_PERMISSION GET_PROPERTY SET_EXECUTE_PRIVILEGE
DERIVEDFROM GET_REPLY SET_FIXED_IN_INSTANCE
DISABLE_OUTPUT_TO_FILE GRANT_PERMISSION SET_NATIVE_COMPILER_OPTION
DISABLE_OUTPUT_TO_JAVA GRANT_POLICY_PERMISSION SET_OUTPUT
DISABLE_OUTPUT_TO_SQL IMPORT_RAW_CHUNK SET_OUTPUT_TO_FILE
DISABLE_OUTPUT_TO_TRC IMPORT_TEXT_CHUNK SET_OUTPUT_TO_JAVA
DISABLE_PERMISSION INITIALIZE_OUTPUT_TO_FILE SET_OUTPUT_TO_SQL
DROPJAVA INIT_BTL SET_PERMISSION_DEBUG
DUMP_NATIVE_MACHINE_CODE LOADJAVA SET_PREFERENCE
ENABLE_OUTPUT_TO_FILE LONGNAME SET_PROPERTY
ENABLE_OUTPUT_TO_JAVA NATIVE_COMPILER_OPTIONS SET_RUNTIME_EXEC_CREDENTIALS
ENABLE_OUTPUT_TO_SQL NCOMP_STATUS_MSG SET_SYSTEM_CLASS_LOADING
ENABLE_OUTPUT_TO_TRC OPTION_CONTROLLER SET_VERIFIER
ENABLE_PERMISSION QUERY_OUTPUT_TO_FILE SHAREDPRIVATECLASSNAME
ENDSESSION QUERY_OUTPUT_TO_JAVA SHORTNAME
ENDSESSION_AND_RELATED_STATE QUERY_OUTPUT_TO_SQL SHOW_PROPERTY
END_EXPORT QUERY_OUTPUT_TO_TRC START_BTL
END_IMPORT REMOVE_OUTPUT_TO_FILE START_EXPORT
EXPORT_CLASS REMOVE_OUTPUT_TO_JAVA START_IMPORT
EXPORT_RAW_CHUNK REMOVE_OUTPUT_TO_SQL START_JMX_AGENT
EXPORT_RESOURCE REMOVE_PROPERTY STOP_BTL
EXPORT_SOURCE RESET_COMPILER_OPTION TERMINATE_BTL
EXPORT_TEXT_CHUNK RESET_PROPERTY_DEFS_TABLE_FLAG UNCOMPILE_CLASS
FINALIZE_OUTPUT_TO_FILE RESOLVER UNCOMPILE_METHOD
FIXED_IN_INSTANCE RESTRICT_PERMISSION UNSET_NATIVE_COMPILER_OPTION
 
DBMS_FEATURE_OJVM
Undocumented dbms_java.dbms_feature_ojvm(
ojvm_boolean OUT NUMBER,
aux_count    OUT NUMBER,
ojvm_info    OUT CLOB);
TBD
 
 
Undocumented dbms_java.dbms_feature_system_ojvm(
ojvm_boolean OUT NUMBER,
aux_count    OUT NUMBER,
ojvm_info    OUT CLOB);
TBD
 
 
Undocumented dbms_java.decode_native_compiler_option(
optionName IN VARCHAR2,
value      IN VARCHAR2)
RETURN VARCHAR2;
TBD
 
DROPJAVA
Drops classes within the database using a call, rather than through the dropjava command-line tool dbms_java.dropjava(options IN VARCHAR2) AS LANGUAGE JAVA NAME 'oracle.aurora.server.tools.loadjava.DropJavaMain.serverMain(java.lang.String)';
exec dbms_java.dropjava('-s rdbms/jlib/schagent.jar');
 
DUMP_NATIVE_MACHINE_CODE
Dump the native code (if available) for the specified method to trace dbms_java.dump_native_machine_code(
classname  IN VARCHAR2,
methodname IN VARCHAR2,
methodsig  IN VARCHAR2);
TBD
 
GET_JDK_VERSION (new 12.1)
Retrieves the database JDK version dbms_java.get_jdk_version RETURN VARCHAR2;
SELECT dbms_java.get_jdk_version
FROM dual;
 
GETVERSION
Retrieves the database version dbms_java.getVersion RETURN VARCHAR2;
SELECT dbms_java.getVersion
FROM dual;
 
GRANT_PERMISSION
Grants Property Permissions. Create an active row in the policy table granting the Permission as specified to grantee/

This demo courtesy of Ralph Mintel. It has been tested against 11.2.0.1 is is valid.
dbms_java.grant_permission(
grantee           IN  VARCHAR2,
permission_type   IN  VARCHAR2,
permission_name   IN  VARCHAR2,
permission_action IN  VARCHAR2,
key               OUT NUMBER)
AS LANGUAGE JAVA NAME 'oracle.aurora.rdbms.security.PolicyTableManager.grant(
java.lang.String, java.lang.String, java.lang.String, java.lang.String, long[])';
-- ==============================================
-- Oracle Java Functions
-- Oracle dba_java_policy
-- Variation on Tom Kyte's java system properties.
-- Ralph Mintel Mar. 21, 2008
-- ==============================================


col name format a40
col value format a60
col id format 999

-- =============================================
-- Function to retrieve all of the java properties.
-- =============================================


CREATE OR REPLACE FUNCTION jp RETURN VARCHAR2 IS LANGUAGE JAVA
  NAME 'java.lang.System.getProperties()
  RETURN java.lang.String';
/

-- =============================================
-- the following will probably fail with ORA-29532:
-- Java call terminated by uncaught Java exception:
-- java.security.AccessControlException:
-- (java.util.PropertyPermission * read,write)
-- has not been granted to UWCLASS.
-- =============================================


SELECT jp() FROM dual;

-- =============================================
-- Grant java PropertyPermission...
-- =============================================

conn sys@pdbdev as sysdba

desc dba_java_policy;

CALL dbms_java.grant_permission('UWCLASS', 'SYS:java.util.PropertyPermission', '*', 'read,write' );

-- =============================================
-- Back to our user...
-- =============================================


conn uwclass/uwclass@pdbdev

col name format a40
col value format a60
col id format 999

SELECT jp() FROM dual;

-- =============================================
-- Create a table for these java properties...
-- =============================================


CREATE TABLE java_properties (
rid   NUMBER,
name  VARCHAR2(40),
value VARCHAR2(660) DEFAULT NULL);

-- =============================================
-- Insert each java property name-value pair into a table.
--
-- The entire block of properties starts with '{'
-- and ends with '}'.
--
-- The property name=value sets are comma delimited.
-- '=' separates the name and value.
-- =============================================


set serveroutput on

DECLARE
 s VARCHAR2(4000);
 x PLS_INTEGER;
 y PLS_INTEGER;

 id           PLS_INTEGER := 1;
 lastone      BOOLEAN := FALSE;
 xname_size   PLS_INTEGER;
 xvalue_start PLS_INTEGER;
 xvalue_size  PLS_INTEGER;
BEGIN
  SELECT jp() INTO s FROM dual;

  x := 2; /* starting column in s, to ignore the opening { */
  y := 1; /* column where comma separator is found. */

  LOOP
    y := INSTR(s, ',', x, 1);
    IF (y < 1) THEN
      y := INSTR(s, '}', x, 1);
      lastone := true;
    END IF;

    dbms_output.put_line(SUBSTR(s, x, y-x));

    xname_size := INSTR(s, '=', x, 1) - x;
    xvalue_start := x + xname_size + 1;
    xvalue_size := INSTR(s, ',', xvalue_start, 1) - xvalue_start;

    IF (lastone) THEN
      xvalue_size := INSTR(s, '}', xvalue_start, 1) - xvalue_start;
    END IF;

    IF (xvalue_size > 0) THEN
      INSERT INTO java_properties
      (rid, name, value)
      VALUES (
      id, SUBSTR(s, x, xname_size), SUBSTR(s, xvalue_start, xvalue_size));
    ELSE
      INSERT INTO java_properties (id, name)
      (rid, name)
      VALUES
      (id, SUBSTR(s, x, xname_size));
    END IF;
    id := id + 1;

    IF (lastone) THEN
      EXIT;
    END IF;

    x := y + 2;
  END LOOP;
END;
/

-- =============================================
-- A mystery...
-- The property 'user.region=US' disappeared.
-- It was there yesterday!
-- Is it on your system? Today? Tomorrow?
-- =============================================


SELECT * FROM java_properties ORDER BY name;

SELECT * FROM java_properties WHERE value IS NULL ORDER BY name;

-- =============================================
-- Revoke java PropertyPermission...
-- =============================================


conn sys@pdbdev as sysdba

CALL dbms_java.revoke_permission('UWCLASS', 'SYS:java.util.PropertyPermission', '*', 'read,write');

set linesize 121
col grantee format a10
col type_schema format a5
col type_name format a30
col name format a20
col action format a20
col enabled format a10

SELECT * FROM dba_java_policy
WHERE grantee = 'UWCLASS';

SELECT COUNT(*) FROM dba_java_policy;

SELECT * FROM dba_java_policy;

-- =============================================
-- Back to our user...
-- =============================================


conn uwclass/uwclass@pdbdev

set pagesize 30
set linesize 121
col name format a40
col value format a60
col id format 999
set serveroutput on
ALTER SESSION SET PLSQL_WARNINGS='ENABLE:ALL';

-- =============================================
-- The following should fail because of revoked permission...
-- =============================================


SELECT jp() FROM dual;

-- =============================================
-- But we still have our java_properties table...
-- =============================================


SELECT * FROM java_properties;

-- =============================================
-- Java & Oracle predefined permissions are described:
-- http://youngcow.net/doc/oracle10g/java.102/b14187/chnine.htm
-- =============================================
 
LOADJAVA
Loads Java classes into the database

See: ?/rdbms/admin/initsjty.sql
dbms_java.loadjava(option IN VARCHAR2) AS LANGUAGE JAVA NAME 'oracle.aurora.server.tools.loadjava.LoadJavaMain.serverMain(java.lang.String)';
exec sys.dbms_java.loadjava('-v -r rdbms/jlib/sqljtype.jar');
 
REVOKE_PERMISSION
Disables any permissions that might have been granted dbms_java.revoke_permission(
grantee           IN VARCHAR2,
permission_type   IN VARCHAR2,
permission_name   IN VARCHAR2,
permission_action IN VARCHAR2)
AS LANGUAGE JAVA NAME 'oracle.aurora.rdbms.security.PolicyTableManager.revoke(
java.lang.String, java.lang.String, java.lang.String, java.lang.String)';
See GRANT_PERMISSION Demo Above
 
SET_OUTPUT
Method for controlling destination of java output dbms_java.set_output(buffersize IN NUMBER);
exec dbms_java.set_output(1000000);
 
SET_RUNTIME_EXEC_CREDENTIALS
Undocumented dbms_java.set_runtime_exec_credentials(
dbuser IN VARCHAR2,
osuser IN VARCHAR2,
ospass IN VARCHAR2);
TBD
 
SHORTNAME
Returns the shorted class name dbms_java.shortname(longname IN VARCHAR2) RETURN VARCHAR2;
SELECT dbms_java.shortname('oracle/mgd/idcode/IDCodeTranslator')
FROM dual;

Related Topics
Java Functions
Packages

Morgan's Library Page Footer
This site is maintained by Dan Morgan. Last Updated: This site is protected by copyright and trademark laws under U.S. and International law. © 1998-2014 Daniel A. Morgan All Rights Reserved