从Java运行SQL * Loader而不使用tnsnames.ora

时间:2016-06-01 15:39:43

标签: java oracle bash escaping sql-loader

我正在尝试通过Unix上的Java运行SQL * Loader。

我正在使用以下命令,当我在命令行(bash)上运行它时它会起作用:

sqlldr userid=user/pass@\"\(DESCRIPTION = \(SDU = xxxx\) \(TDU = xxxx\) \(ADDRESS_LIST = \(ADDRESS = \(PROTOCOL = TCP\)\(HOST = xxxxx\)\(PORT = xxxx\)\) \(LOAD_BALANCE = on\) \(FAILOVER = on \) \)\(CONNECT_DATA = \(SERVICE_NAME = xxxx\) \)\)\" control='xxxx' log='xxxx' readsize=xxxx streamsize=xxxx 

我正在使用转义来run it without a tnsnames.ora file

这就是我在Java代码中翻译的方式:

String unixCmd = "sqlldr userid=user/pass@\\\"\\(DESCRIPTION = \\(SDU = xxxx\\) \\(TDU = xxxx\\) \\(ADDRESS_LIST = \\(ADDRESS = \\(PROTOCOL = TCP\\)\\(HOST = xxxx\\)\\(PORT = xxxx\\)\\) \\(LOAD_BALANCE = on\\) \\(FAILOVER = on \\) \\)\\(CONNECT_DATA = \\(SERVICE_NAME = xxxx\\) \\)\\)\\\" control='xxxx' log='xxxx' readsize=xxxx streamsize=xxxx";
System.out.println(unixCmd); 
p = Runtime.getRuntime().exec(unixCmd);//not working
p = Runtime.getRuntime().exec("sh -c "+unixCmd);//not working 

我收到以下错误消息:

LRM-00116: syntax error at 'user/pass' following '='
SQL*Loader: Release 11.2.0.2.0 - Production on Wed Jun 1 11:29:59 2016
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

我做错了什么?

1 个答案:

答案 0 :(得分:0)

你过度逃避了。在shell脚本中,您需要转义括号,因为您不希望shell本身解释这些;在Java字符串中你不要:

String unixCmd = "sqlldr userid=user/pass@\"(DESCRIPTION = (SDU = xxxx) (TDU = xxxx) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = xxxx)(PORT = xxxx)) (LOAD_BALANCE = on) (FAILOVER = on ) )(CONNECT_DATA = (SERVICE_NAME = xxxx) ))\" control='xxxx' log='xxxx' readsize=xxxx streamsize=xxxx";
System.out.println(unixCmd); 
p = Runtime.getRuntime().exec(unixCmd);

只有双引号需要转义。使用此代码,它无法获得LRM-00116。使用您的虚拟值时,它当然仍会出错 - 最初在LRM-00104: 'xxxx' is not a legal integer for 'readsize',但是输入实际值会显示它按预期工作。