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

前端 未结 4 1377
悲哀的现实
悲哀的现实 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:38

    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 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();  
      }  
    }  
    

提交回复
热议问题