1

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 is as follows:

public static void test_script () {  

        String fileName = "@t.sql";  
        //t.sql contains "show user" command
        String sqlPath = "D:\\";  

        String sqlCmd = "sqlplus";  
       // String arg1   = "scott/tiger@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname)(Port=PORT ID))(CONNECT_DATA=(SID=SID)))";
        String arg1   = "sys as sysdba/tiger@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname)(Port=PORT ID))(CONNECT_DATA=(SID=SID)))";

       //String arg1="/ as sysdba";

        String arg2= fileName;
        //String arg2="conn /as sysdba";
        try {  
            String line;  
            ProcessBuilder pb = new ProcessBuilder(sqlCmd, arg1,arg2);  
            Map<String, String> env = pb.environment();  
            env.put("VAR1", arg1);  
            env.put("VAR2", arg2);  
            //env.put("VAR3", arg3);
            pb.directory(new File(sqlPath));  
            pb.redirectErrorStream(true);  
            Process p = pb.start();  
            BufferedReader bri = new BufferedReader  
            (new InputStreamReader(p.getInputStream()));  
            BufferedReader bre = new BufferedReader  
            (new InputStreamReader(p.getErrorStream()));  
            while ((line = bri.readLine()) != null) {  
            System.out.println(line);  
            }  
            bri.close();  
            while ((line = bre.readLine()) != null) {  
            System.out.println(line);  
            }  
            bre.close();  
            System.out.println("\n\n\n");
            System.out.println("Done.");  
            }  
            catch (Exception err) {  
                err.printStackTrace();  
            }  
        }  
   } 

When I try to run this code, I find this error:

SQL*Plus: Release 11.2.0.1.0 Production on Thu Apr 10 11:08:59 2014

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


SQL*Plus: Release 11.2.0.1.0 Production

Copyright (c) 1982, 2010, Oracle.  All rights reserved.

Use SQL*Plus to execute SQL, PL/SQL and SQL*Plus statements.

Usage 1: sqlplus -H | -V

    -H         Displays the SQL*Plus version and the
           usage help.
    -V         Displays the SQL*Plus version.

Usage 2: sqlplus [ [<option>] [{logon | /nolog}] [<start>] ]
...

... and the rest of the SQL*Plus 'usage' information.

Am I supplying wrong arg1 argument or is there any other way of connecting as SYS in Oracle through Java.

Alex Poole
  • 183,384
  • 11
  • 179
  • 318
saurabhk
  • 140
  • 1
  • 4
  • 14

4 Answers4

3

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

sqlplus "sys as sysdba/tiger@<connect_string>"

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@<connect_string>"

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.

Alex Poole
  • 183,384
  • 11
  • 179
  • 318
  • It show this ERROR:ORA-12560: TNS:protocol adapter error – saurabhk Apr 10 '14 at 07:57
  • Are your host, port and SID right; matching how you were able to connect as `scott`? – Alex Poole Apr 10 '14 at 07:59
  • My host,port and SID are correct ,but still can't connect as sys but as normal user i am able to connect – saurabhk Apr 10 '14 at 08:16
  • @saurabhk - I've had a chance to test this now and it works fine for me. Without seeing the actual argument values when you connect as `scott` and as `sys` I'm not sure where the issue might be. If you're using the same connect-string then I don't see how one can work and the other not, with that error. – Alex Poole Apr 10 '14 at 08:47
  • 1
    It should also be noted that spaces in the command line are a nightmare. On Windows, the command line is passed to the process as a single string and the process itself must divide this into arguments. This differs from unix-like OSes which already receive an array of the arguments. Java's ProcessBuilder add its own magic. And as Alex pointed out, it does not sound like a good idea to run SQL Plus as SYSDBA from within a Java program anyway. – hvb Apr 10 '14 at 09:02
  • Hey @AlexPoole could you please give me ur mail so that i can have a chat and u could help me out bro. – saurabhk Apr 10 '14 at 09:09
1

I found the answer by hit and try and its the connection string.

If one wants to connect as sysdba/sysoper the connection string should be like:

public static void test_script () {  
  String fileName = "@t.sql";  
  String sqlPath = "D:\\";  
  String sqlCmd = "sqlplus";  
  // IP_address,portid and sid are variables to be entered and t.sql is the file to be read .It contains show user command 
  String arg3   = "sys/oracle123@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=IP_address)(Port=portid))(CONNECT_DATA=(SID=sid))) as sysdba";
  String arg4= fileName;
  try {  
    String line;  
    ProcessBuilder pb = new ProcessBuilder(sqlCmd,arg3,arg4);
    Map<String, String> env = pb.environment();  
    env.put("VAR3", arg3);
    env.put("VAR4", arg4);
    pb.directory(new File(sqlPath));  
    pb.redirectErrorStream(true);  
    Process p = pb.start();  
    BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));  
    BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));  
    while ((line = bri.readLine()) != null) {  
        System.out.println(line);  
    }       
    bri.close();  
    while ((line = bre.readLine()) != null) {  
        System.out.println(line);  
    }  
    bre.close();  
    System.out.println("\n\n\n");
    System.out.println("Done.");  
  } catch (Exception err) {  
    err.printStackTrace();  
  }  
}  
True Soft
  • 8,675
  • 6
  • 54
  • 83
saurabhk
  • 140
  • 1
  • 4
  • 14
0

Think your arg1 should be as below:

              String arg1   = "scott as sysdba/<syspwd>@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname)(Port=PORT ID))(CONNECT_DATA=(SID=SID)))";
Charles
  • 50,943
  • 13
  • 104
  • 142
vishad
  • 1,134
  • 7
  • 11
0

You can easily call sqlplus from java like below.

String stringCommand =
            "sqlplus " + dbUser + "/" + dbPassword + "@(description=(address=(protocol=TCP)" +
                    "(host=" + dbHost + ")(port=" + dbPort + "))(connect_data=(service_name=" + dbName + "))) " +
                    "@" + sqlScriptFile + "";       

    int exitCode;
    Runtime rt = Runtime.getRuntime();
    Process process = null;
    try {
        process = rt.exec(stringCommand);
        exitCode = process.waitFor();
    } finally {
        process.destroy();
    }
Shehan Simen
  • 1,046
  • 1
  • 17
  • 28