Skip to content

Commit e37d0b3

Browse files
authored
Add files via upload
1 parent 90924e5 commit e37d0b3

File tree

2 files changed

+84
-21
lines changed

2 files changed

+84
-21
lines changed

java/ojvm/README.md

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
# OJVM based examples
2-
This folder stores Java based examples for the embedded JVM of the Oracle Database (a.k.a. OJVM). We are referring to plain Java code
3-
with embedded SQL statements, similar to client JDBC code.
4-
5-
The motivations for running Java code directly in the database include:
6-
* reusing Java code, Java libraries, and Java skills (developers) for database modules written in Java thereby allowing the same
7-
language across the mid-tier and the database-tier. The embedded JVM (a.k.a. OJVM also allows JavaScript (see our examples), Scala
8-
(see [Igor Racic's examples](http://www.igorandsons.com/)). I also described in [chapter 5 of my book](https://www.amazon.com/dp/1555583296), how to run run Jython, Jacl, Scheme, and Groovy in the database
9-
* the other key reason for running Java directly in the database is performance; really? Yes, even if HotSpot or external JVMs run
10-
pure Java sligthly faster than OJVM (but no one runs pure Java code i.e., Fibonnaci computation in the database), running Java in
11-
the database cuts the network traffic incurred by the steps involved in processing SQL statements (i.e., parse, and/or bind, and/or
12-
execute, and fetches).
13-
You can see for yourself with the Trimlob and Workers examples (running both inside and outside the database).
14-
15-
* OJVM allows calling out external SOAP and REST Web Services. [See the landing page for more details](http://www.oracle.com/technetwork/database/database-083829.html)
16-
17-
## [Documentation](http://docs.oracle.com/database/122/JJDEV/toc.htm)
18-
19-
## [What's in Oracle database 12c Release 2 for Java & JavaScript Developers?](http://bit.ly/2orH5jf)
20-
## [OTN Landing page](http://www.oracle.com/technetwork/database/application-development/java-db/overview/index.html)
21-
## [Community Forum](https://community.oracle.com/community/database/developer-tools/jvm)
1+
2+
# Nashorn based examples
3+
Java 8 furnishes Nashorn, a JavaScript engine which runs on the JVM including JDK, JRE, and the embeded JVM in the Oracle database a..k.a. OJVM.
4+
This folder stores database related Nashorn based examples (i.e., plain JavaScript + SQL statements) including JavaScript Stored procedures with OJVM, and standalone/client JavaScript functions with JDK/JRE.
5+
6+
For data access, Nashorn allows interoperability between Java and JavaScript, therefore, the SQL statements are invoked using JDBC which is a portable standard Java API.
7+
In addition to SQL statements, for JSON collections and documents, you can also use the fluent API (i.e., no SQL, dot notation) using SODA for Java with JavaScript, the beauty of Nashorn (see SODAjs.md for more details).
8+
9+
For JavaScript Stored Procedures, the steps are very simple
10+
1) create/design your JavaScript function in a file
11+
2) load the JavaScript file into your database schema using the loadjava utility (DBMSJAVASCRIPT role required)
12+
3) invoke it using (i) either DBMS_JAVASCRIPT.run(<JS file>) or (ii) DbmsJavaScript.run Java call, or (iii) using javax.script API
13+
14+
[See my blog post for more details](http://db360.blogspot.com/search?updated-max=2016-11-09T08:41:00-08:00&max-results=3)
15+
16+
[Documentation](http://bit.ly/2nstiYQ)
17+
18+
[White paper](http://bit.ly/2orH5jf) What's in Oracle database 12c Release 2 for Java & JavaScript Developers?
19+
20+
[Community Forum](https://community.oracle.com/community/database/developer-tools/jvm)

java/ojvm/ReadMe-JavaScript-OJVM.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Running JavaScript in Oracle database
2+
3+
The embedded JVM in Oracle database 12 Release 2 (DB 12.2.0.1) supports Java SE 8 therefore the Nashorn engine.
4+
5+
## Steps for running JavaSript in the Oracle database, using Nashorn on OJVM.
6+
7+
* **load the JavaScript file in the database as a Java Resource**
8+
loadjava -v -u username/password hello.js <-- replace hello.js by your JS file
9+
* **Run/Execute the file previously loaded using one of the following 3 methods**
10+
11+
a) Running JavaScript in the Database using DBMS_JAVA.JAVASCRIPT.RUN Procedure
12+
From SQL or PL/SQL
13+
SQL>set serveroutput on
14+
SQL>call dbms_java.set_output(20000);
15+
SQL>call dbms_javascript.run("hello.js");
16+
17+
b) Running JavaScript in the Database using DbmsJavaScript.run Java call
18+
19+
From Java, running in the database (OJVM)
20+
import oracle.aurora.rdbms.DbmsJavaScript;
21+
22+
DbmsJavaScript.run("hello.js");
23+
24+
c) Running JavaScript in the Database using the javax.script API
25+
This approach consists in 4 simple steps (described hereafter) and gives you more flexibility, specifically for functions or procedures accepting a variable number of parameters and/or returning value(s).
26+
Notes: The direct invocation of Nashorn classes is restricted in Oracle JVM. All scripting mode extensions are disabled in Oracle JVM.
27+
28+
## Steps for invoking JavaScript in the database using the javax.script API
29+
* **Instantiate a script manager**
30+
import javax.script.*;
31+
import java.net.*;
32+
import java.io.*;
33+
...
34+
ScriptEngineManager factory = new ScriptEngineManager();
35+
36+
* ** 1.Create an engine**
37+
ScriptEngine engine = factory.getEngineByName("myJSengine");
38+
39+
* ** 2.Pass your resource stream reader as the argument to the eval method of the engine**
40+
URL url =
41+
Thread.currentThread().getContextClassLoader().getResource("hello.js");
42+
engine.eval(new InputStreamReader(url.openStream()));
43+
...
44+
45+
* ** 3.Invoke a function of the JavaScript code
46+
Invocable invocable = (Invocable) engine;
47+
Object selectResult =
48+
invocable.invokeFunction("selectQuery", inputId);
49+
50+
## Turn the javax.script steps into a javax script wrapper class
51+
The furnished InvokeScript.java has all these steps.
52+
The signature may differ depending on your input parameters and return values.
53+
The following SQL script creates the Java class in your schema
54+
55+
create or replace and compile java resource named "InvokeScript" as
56+
@InvokeScript.java
57+
/
58+
Alrnatively you can simply load the Java class in your schema using
59+
loadjava -r -v -user hr/hr InvokeScript.java
60+
61+
## Create a SQL wrapper for the javax.script wrapper class
62+
CREATE OR REPLACE FUNCTION invokeScriptEval(inputId varchar2) return varchar2 as language java
63+
name 'InvokeScript.eval(java.lang.String) return java.lang.String';
64+
/

0 commit comments

Comments
 (0)