我想从Java中的SQL * Plus连接到Oracle的SYS
。但我无法连接。但我能够以名为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
那样经常连接,这不是一个好主意 - 但也许这只是一个学习练习。
我通过命中找到答案并尝试连接字符串。
如果想要以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();
}
}
认为您的arg1应如下所示:
String arg1 = "scott as sysdba/<syspwd>@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname)(Port=PORT ID))(CONNECT_DATA=(SID=SID)))";