Search code examples
javaoracle-databasenavicat

Escape semi colon in "CREATE OR REPLACE AND COMPILE JAVA" statement


I'm trying to execute the following statement in Navicat,

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED util
AS
import java.io.IOException;

public class Util
{
    public static String exec(String cmd) throws IOException
    {
        Runtime.getRuntime().exec(cmd);
        return "";
    }
}

But since ; is treated as a delimiter in SQL query, the query became "incomplete" and results in an error:

enter image description here

So how can I execute this statement in Navicat?


Solution

  • Store the command as a string and dynamically execute it with an anonymous PL/SQL block:

    begin
        execute immediate
        '
            CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED util
            AS
            import java.io.IOException;
    
            public class Util
            {
                public static String exec(String cmd) throws IOException
                {
                    Runtime.getRuntime().exec(cmd);
                    return "";
                }
            }
        ';
    end;
    /
    

    Not many Oracle IDEs understand the Java syntax, but I would assume that almost all of them can work with PL/SQL blocks.