0

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.

6
  • 2
    Your database has nothing to do with the windows username of any client user. If you need a windows username in the database, pass it from your client. The database cannot do that. Commented Sep 16, 2019 at 8:19
  • Why do you need to create a procedure when you have the java file which gives you the user name. Anyway you can call the java source code using the process like this in oracle. docs.oracle.com/cd/B14117_01/java.101/b12021/invokeapp.htm Commented Sep 16, 2019 at 8:25
  • @mallikarjun but then you will get the user name of the sql process. Assuming that the sql server is even running on windows. Commented Sep 16, 2019 at 8:42
  • @f1sh ok, but, how can i do that? Commented Sep 16, 2019 at 8:44
  • @mallikarjun I will try this, just need to load the class into db. Thank you for advice. Commented Sep 16, 2019 at 8:44

1 Answer 1

2

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';
/
Sign up to request clarification or add additional context in comments.

1 Comment

This example works out fine, so far no errors occurred when compiling. Can you write me how to call this function using sql? Because when I tried to call the function, I got an error: ORA-00932: inconsistent datatypes: expected a Java type at argument position 2 to which some Oracle value can be converted got something else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.