How to connect to Oracle as “SYS” from SQL*Plus in Java

前端 未结 4 1420
悲哀的现实
悲哀的现实 2021-01-25 02:41

I want to connect to Oracle as SYS from SQL*Plus in Java. But I am not able to connect. But I am able to connect as user named SCOTT. My code snippet i

4条回答
  •  不知归路
    2021-01-25 03:27

    You're passing all the connection information as a single value; equivalent to this from a command line:

    sqlplus "sys as sysdba/tiger@"
    

    which would get the same response of printing the SQL*Plus logon help. You also have your password in the wrong place but it isn't getting that far. From a command line this would work:

    sqlplus "sys/tiger" "as" "sysdba@"
    

    so you need to pass 5 arguments to ProcessBuilder, something like:

        String sqlCmd = "sqlplus";  
        String arg1   = "sys/tiger";
        String arg2   = "as";
        String arg3   = "sysdba@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname)(Port=PORT ID))(CONNECT_DATA=(SID=SID)))";
        String arg4   = fileName;
        ...
            ProcessBuilder pb = new ProcessBuilder(sqlCmd, arg1, arg2, arg3, arg4);
    

    This will still only work if your environment is configured to allow remote connection as sysdba. Doing anything as sys should be very rare, and having a script you want to run as sys seem unusual enough for a Java wrapper to seem like overkill - and makes it seem like you might connect as sys routinely, which is not a good idea - but maybe this is just a learning exercise.

提交回复
热议问题