0 Replies Latest reply: Apr 6, 2012 7:41 AM by 928948 RSS

    How to reference multiple instances of the same Java object from PL/SQL?

    928948
      Dear all,

      I'm experimenting with calling Java from PL/SQL.

      My simple attempts work, which is calling public static [java] methods through PL/SQL wrappers from SQL (and PL/SQL). (See my example code below).

      However it is the limitation of the public static methods that puzzels me.

      I would like to do the following:
      - from PL/SQL (in essence it needs to become a forms app) create one or more objects in the java realm
      - from PL/SQL alter properties of a java object
      - from PL/SQL call methods on a java object

      However I fail to see how I can create multiple instances of an object and reference one particular object in the java realm through public static methods.

      My current solution is the singleton pattern: of said java object I have only 1 copy, so I do not need to know a reference to it.
      I can just assume that there will only ever be 1 of said object.
      But I should be able to make more then 1 instance of an object.

      To make it more specific:
      - suppose I have the object car in the java realm
      - from PL/SQL I want to create a car in the java realm
      - from PL/SQL I need to give it license plates
      - I need to start the engine of a scpecific car

      However if I want more then 1 car then I need to be able to refrence them. How is this done?
      Somehow I need to be able to execute the following in PL/SQL:

      DECLARE
      vMyCar_Porsche CAR;
      vMyCar_Fiat CAR;

      BEGIN

      vMyCar_Porsche = new CAR();
      vMyCar_Fiat = new CAR();

      vMyCar_Porsche.setLicensePlates('FAST');
      vMyCar_Porsche.startEngine();

      vMyCar_Fiat.killEngine();

      END;

      Thanks in advance.

      Best Regards,
      Ruben


      My current example code is the following:

      JAVA:
      ===
      CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED CODAROUL."RMG/BO/RMG_OBJECT" as package RMG.BO;


      public class RMG_OBJECT {

      private static RMG_OBJECT instance = new RMGOBJECT();

      private String rmgObjectNaam;


      private RMG_OBJECT(){
      this.rmgObjectNaam = "NonDetermined";
      }


      public static String GET_RMGOBJECT_NAAM () {
      String toestand = null;
      if (_instance == null) {toestand = "DOES NOT EXIST";} else { toestand = "EXISTS";};

      System.out.println("instance : " + toestand);
      System.out.println("object name is : " + _instance.rmgObjectNaam);

      return _instance.rmgObjectNaam;
      }

      public static Integer SET_RMGOBJECT_NAAM (String IN)
      {
      try
      {
      _instance.rmgObjectNaam = IN;
      return 1;
      }
      catch (Exception e)//catch
      {
      System.out.println("Other Exception: " + e.toString());
      e.printStackTrace();
      return 5;
      } //catch
      }

      }
      /

      PL/SQL Wrapper:
      ==========

      CREATE OR REPLACE FUNCTION CODAROUL.SET_RMGOBJECT_NAAM(NAAM IN VARCHAR2) return NUMBER AS
      LANGUAGE JAVA NAME 'RMG.BO.RMG_OBJECT.SET_RMGOBJECT_NAAM (java.lang.String) return java.lang.Integer';
      /

      Calling from SQL:
      ==========

      CALL dbms_java.set_output(2000);


      select CODAROUL.GET_RMGOBJECT_NAAM() from dual;

      Edited by: RubenS_BE on Apr 6, 2012 5:35 AM

      Edited by: 925945 on Apr 6, 2012 5:41 AM