Calling Java class inside Oracle

Asked

Viewed 1,144 times

2

would like to know if it is possible to call a Java class inside the Oracle database.

I was able to install the JVM inside the Oracle server already, but I cannot compile the Java class.

Does anyone know any way I can do that?

  • 1

    What is the purpose of "calling" a Java class within Oracle?

  • 1

    Probably the question is how to execute an application code when an event occurs in the database, for example, if another system inserts data into a given table. This is a common and recurring question. The best answer I have so far is to read the database uninterruptedly at a certain time interval. An alternative would be to make the system that inserts the record "warn" the application through a post-recording call. However, the ideal would be to abandon this technique and go to Restful Web Services.

  • @utluiz seriously that you interpreted this? I am breaking my head for a long time and only managed to interpret absurd things, rs.. I was trying to write a comment asking for clarification but even this was difficult. Erico, could you say if this is what the utluiz deduced? It would be good if you could improve the text of your question.

  • @Math It’s a kick. In fact, I know that Oracle actually has an integration with Java, just as SQL Server has integration with C#, where it is possible to extend the database with new features. But at least in the case of the Oracle, I’ve never seen anyone do it.

  • @Andrade the purpose is to make use of some package from Pentaho for the business rule I need. I need to process a file in the database, generated by Pentaho

1 answer

4


Yes, it is possible to call Java code within the Oracle database. Following is link to official documentation: http://docs.oracle.com/database/121/JJDEV/chthree.htm#JJDEV13167.

See Oracle example, you should follow the following steps:

  1. Set a java class:

    public class Hello
    {
      public static String world()
      {
        return "Hello world";
      }
    }
    
  2. Compile class normally using a JDK:

    javac Hello.java
    
  3. Open the compiled java class within the database:

    loadjava -user MEUUSER Hello.class
    
  4. Create the call for the Java method:

    CREATE OR REPLACE FUNCTION helloworld RETURN VARCHAR2 AS
    LANGUAGE JAVA NAME 'Hello.world () return java.lang.String';
    /
    

You can consume the Java function directly from PL/SQL as an example from Oracle:

 VARIABLE myString VARCHAR2(20);
 CALL helloworld() INTO :myString;
 PRINT myString;

However, as per utluiz’s comment, keep in mind that consuming a Java class directly from Oracle is not always the best architectural decision. It is often better to expose a Web Service and consume it through the bank. This can be done, for example, with the package UTL_HTTP.

  • @Anthony.Accioly, thanks man, with this I managed to do what I needed. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.