0

When using Oracle SQL Developer, I noticed that it has a 'Java' Directory (as in the image).

SQL Developer - Java Directory

I assumed that the purpose of it is for storing Java source/class code in the DB. I also assumed that means Java code can be executed from within the DBMS. I looked at the help documentation for evidence of this, but only found the following, under Java Sources:

Java sources can be created and managed in the database.

It's not really clear to me if any of the following (other than 1) are true:

  1. It's possible to store Java source code/classes within a database.
  2. Java code/applications can be run from within the RDBMS environment, not just externally to it.
  3. Plugins/extensions/add-on modules for the DBMS can be written in Java.

Which (if any) of statements 2 and 3 is true? What is the purpose of that directory if not only for storage? is there an advantage to storing source code in the DB instead of using a version control tool such as git?

Agi Hammerthief
  • 2,114
  • 1
  • 22
  • 38
  • Are you really asking about SQL Developer, or about [Java in the database](https://docs.oracle.com/en/database/oracle/oracle-database/12.2/jjdev/Java-introduction.html)? – Alex Poole Jul 17 '18 at 06:53
  • @AlexPoole: I'm asking if that feature is specific to Oracle DB or SQL Developer (as an IDE) and what it's purpose is: Storage only or something more. Thank you for the link. I'll read through that and see if it answers my question. – Agi Hammerthief Jul 17 '18 at 06:57

2 Answers2

3

Thank you to Alex Poole for links to the Oracle documentation, which states the following:

From Oracle's "Using Java" documentation:

You can write and load Java applications within the database because it is a safe language with a lot of security features.

[...] clients call Java stored procedures the same way they call PL/SQL stored procedures.

From Oracle's Java Programming in Oracle Database documentation:

Java stored procedures as the Java equivalent and companion for PL/SQL: Java stored procedures are tightly integrated with PL/SQL. You can call Java stored procedures from PL/SQL packages and PL/SQL procedures from Java stored procedures.

Java stored procedures are Java programs written and deployed on a server and run from the server, exactly like a PL/SQL stored procedure. You invoke it directly with products like SQL*Plus, or indirectly with a trigger. You can access it from any Oracle Net client, such as OCI and PRO*, or JDBC or SQLJ.

In addition, you can use Java to develop powerful, server-side programs, which can be independent of PL/SQL. Oracle Database provides a complete implementation of the standard Java programming language and a fully compliant JVM.

You can call existing PL/SQL programs from Java and Java programs from PL/SQL. This solution protects and leverages your PL/SQL and Java code and opens up the advantages and opportunities of Java-based Internet computing.

Oracle Database offers two different Java APIs for accessing SQL data, JDBC and SQLJ. Both these APIs are available on the client, and the JDBC API is also available on the server. As a result, you can deploy your applications on the client and server.

From the "Stored Procedures and Runtime Contexts" documentation:

Stored procedures are Java methods published to SQL and stored in the database for general use. To publish Java methods, you write call specifications, which map Java method names, parameter types, and return types to their SQL counterparts.

When called by client applications, a stored procedure can accept arguments, reference Java classes, and return Java result values.

You can run Java stored procedures in the same way as PL/SQL stored procedures. Usually, a call to a Java stored procedure is a result of database manipulation, because it is usually the result of a trigger or SQL DML call. To call a Java stored procedure, you must publish it through a call specification.

This leads me to the understanding that:

  1. The Java directory is part of the DB itself, not SQL Developer.
  2. The purpose of this directory is only for storing Java classes that define stored procedures (and their helper methods) via a callable function, such as below:1

public class Oscar {
  // return a quotation from Oscar Wilde
  public static String quote() {
    return "I can resist everything except temptation.";
  }
}
CREATE FUNCTION oscar_quote RETURN VARCHAR2 AS LANGUAGE JAVA
NAME 'Oscar.quote() return java.lang.String';
VARIABLE theQuote VARCHAR2(50);
CALL oscar_quote() INTO :theQuote;
PRINT theQuote;

If the program uses libraries external to the standard JVM, they first need to be loaded through the loadjava command-line utility. For example:

loadjava -user USERNAME/password@sid -resolve /path/to/externalLib.jar

  1. Running Java Stored Procedures
MT0
  • 143,790
  • 11
  • 59
  • 117
Agi Hammerthief
  • 2,114
  • 1
  • 22
  • 38
  • 1
    The preconditions on when you can use Java are wrong. The program can use any libraries that you want - just use the [`loadjava` command-line utility](https://docs.oracle.com/en/database/oracle/oracle-database/18/jjdev/loadjava-tool.html) to add the libraries to the database (also see this question where using [`loadjava` is the answer](https://stackoverflow.com/a/46581710/1509264)). Any return value can be supported including `CLOB`, `BLOB`, `OBJECT` and Oracle collections/varrays (Java arrays). – MT0 Jul 17 '18 at 09:06
  • @MT0 Thank you for pointing out the errors. I have edited my answer to incorporate the point about using `loadjava`, although I am not familiar with it. Feel free to correct the answer if you feel that it is still shows incorrect conditions or needs expansion. – Agi Hammerthief Jul 17 '18 at 09:19
1

The Oracle Database allows for calling Java code from PL/SQL - details here. Any Java code uploaded to the database will show in SQL Developer in Java directory.

Konrad Botor
  • 4,765
  • 1
  • 16
  • 26