javawindowsoracle-databasejava-stored-procedures

Call Java SOURCE using function


I need to find out the Windows username using SQL, PL/SQL or Java. So far I've found some java code that can return a Windows username. I put the Java code in my schema database, but can't create a function that will call this Java code.

So, my question is:

How can I write an SQL function that will call this Java code below?

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED TED."`GetWindowUsername`" as import java.`lang`.*;
import java.`util`.*;
import java.`io`.*;

public class `GetWindowUsername` {
    public static final void main( String `args`[] ) {
        String `userName` = System.get Property("user.name");
        System.out.`println`("Window's Username: "+user Name);
    }
}

I found this code on the internet as an example for getting Windows username.

I'm using Oracle Toad as a tool for SQL coding.


Solution

  • How can I write an SQL function that will call this JAVA code below?

    You need a class containing a static method that will return a value (another example of this):

    CREATE AND COMPILE JAVA SOURCE NAMED GetWindowUsername AS
    public class GetWindowUsername {
      public static String getUsername()
      {
        return java.lang.System.getProperty("user.name");
      }
    }
    

    Then you need to create a PL/SQL function to call the Java class's function:

    CREATE FUNCTION getUsername( in_value IN VARCHAR2 ) RETURN VARCHAR2
    AS LANGUAGE JAVA NAME 'GetWindowUsername.getUsername() return java.lang.String';
    /