如何使用Java中的SQL * Plus以“SYS”身份连接到Oracle

时间:2014-04-10 04:17:37

标签: java oracle sqlplus processbuilder

我希望从Java中的SQL * Plus以SYS身份连接到Oracle。但我无法连接。 但我能够以名为SCOTT的用户身份进行连接。我的代码段如下:

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

当我尝试运行此代码时,我发现此错误:

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>] ]
...

...以及其他SQL * Plus'使用'信息。

我是否提供了错误的arg1参数,或者是否有任何其他方式通过Java在Oracle中连接为SYS

4 个答案:

答案 0 :(得分:3)

您将所有连接信息作为单个值传递;从命令行等效于此:

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

将获得与打印SQL * Plus登录帮助相同的响应。您的密码也在错误的位置,但它没有那么远。从命令行开始,这将起作用:

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

所以你需要将5个参数传递给ProcessBuilder,例如:

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

仅当您的环境配置为允许远程连接为sysdba时,这仍然有效。做任何事情sys应该是非常罕见的,并且让你想要以sys运行的脚本看起来不同寻常,以至于Java包装器看起来有点矫枉过正 - 并使你看起来可能像{{{常规地,这不是一个好主意 - 但也许这只是一个学习练习。

答案 1 :(得分:1)

我通过点击找到答案并尝试连接字符串。

如果想要以sysdba / sysoper身份连接,则连接字符串应为:

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

答案 2 :(得分:0)

认为您的arg1应如下所示:

              String arg1   = "scott as sysdba/<syspwd>@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname)(Port=PORT ID))(CONNECT_DATA=(SID=SID)))";

答案 3 :(得分:0)

您可以像下面那样从Java轻松调用sqlplus。

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