Oracle DBMS_JAVASCRIPT
Version 12.2.0.1

General Information
Library Note Morgan's Library Page Header
The Library is currently in the process of being upgraded from Oracle Database Version 12.1.0.2 to 12.2.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.

Also important ... there are rumors on the web about SPARC and Solaris being discontinued. Welcome to the age of fake news. There is absolutely not a byte of truth to these irresponsible rumors.
Purpose Used to execute a Java Script file stored in the file system.
AUTHID CURRENT_USER
Dependencies STANDARD only
Documented No
Exceptions
Error Code Reason
ORA-29532 Java call terminated by uncaught Java exception: java.lang.Error: No such resource found
First Available 12.2.0.1
Security Model Owned by SYS with EXECUTE granted to PUBIC
Source {ORACLE_HOME}/rdbms/admin/initdbj.sql
 
RUN (new 12.2)
Run a java script dbms_javascript.run(script_name IN VARCHAR2);
exec dbms_javascript.run('/home/oracle/jscripts/uwprocess.js');
 
Demo
Full package demo -- create file and store at $ORACLE_HOME/rdbms/jlib
var Driver = Packages.oracle.jdbc.OracleDriver;
var oracleDriver = new Driver();
var url = "jdbc:default:connection:"; // server-side JDBC driver
var query ="SELECT first_name, last_name from employees";
// Establish a JDBC connection
var connection = oracleDriver.defaultConnection();
// Prepare statement
var preparedStatement = connection.prepareStatement(query);
// execute Query
var resultSet = preparedStatement.executeQuery();
// display results
while(resultSet.next()) {
print(resultSet.getString(1) + " == " + resultSet.getString(2) + " " );
}
// cleanup
resultSet.close();
preparedStatement.close();
connection.close();



SQL> CREATE ROLE c##nashorn;

Role created.

SQL> exec dbms_java.grant_permission('C##NASHORN', 'SYS:java.lang.RuntimePermission', 'createClassLoader', '' );

Call completed.

SQL> exec dbms_java.grant_permission('C##NASHORN', 'SYS:java.lang.RuntimePermission', 'getClassLoader', '' );

PL/SQL procedure successfully completed.

SQL> exec dbms_java.grant_permission('C##NASHORN', 'SYS:java.util.logging.LoggingPermission', 'control', '' );

PL/SQL procedure successfully completed.

GRANT c##nashorn TO hr;

Grant succeeded.

SQL> exec dbms_java.loadjava('-v -r rdbms/jlib/database.js');

PL/SQL procedure successfully completed.

SQL> hr/hr@orcl
SQL> serveroutput on
SQL> exec dbms_java.set_output(80000);
PL/SQL procedure successfully completed.

SQL> exec dbms_javascript.run('rdbms/jlib/database.js'); Dan == Morgan

PL/SQL procedure successfully completed.

SQL> exec dbms_java.dropjava('-s rdbms/jlib/database.js');

PL/SQL procedure successfully completed.

Related Topics
Built-in Functions
Built-in Packages
DBMS_JAVA
What's New In 12cR1
What's New In 12cR2

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-2017 Daniel A. Morgan All Rights Reserved