ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2019-10-13 16:10:10  阅读:313  来源: 互联网

标签:processbuilder java oracle sqlplus



我想从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连接.

解决方法:

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

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包装程序看起来像是过分杀人了-并使其看起来像您可能以sys常规方式连接,这不是一个好主意-但这也许只是学习练习.



标签:processbuilder,java,oracle,sqlplus

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有