我用ProcessBuilder编写了一个java程序来启动CMD提示并连接SQLPLUS来执行一些SQL文件 .
public class OracleConnect {
public static void main(String[] args) throws IOException {
String[] cmd = new String[] { "sqlplus", "/@" };
ProcessBuilder builder = new ProcessBuilder(cmd);
Process process = builder.start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
new ReadFromConsole(br).start();
new WriteToConsole(bw).start();
}
}
class AbsThread extends Thread {
boolean read = true;
boolean write = false;
}
class ReadFromConsole extends AbsThread {
BufferedReader processOut;
public ReadFromConsole(BufferedReader processOut) {
this.processOut = processOut;
}
@Override
public void run() {
while (read) {
int i = -1;
try {
while ((i = processOut.read()) != -1) {
char ch = (char) i;
System.out.print(ch);
}
} catch (IOException e) {
e.printStackTrace();
}
write = true;
read = false;
}
}
}
class WriteToConsole extends AbsThread {
BufferedWriter processIn;
public WriteToConsole(BufferedWriter processIn) {
this.processIn = processIn;
}
@Override
public void run() {
while (write) {
System.out.print("Enter command ");
String cmd = new Scanner(System.in).nextLine();
try {
processIn.write(cmd);
processIn.flush();
} catch (IOException e) {
e.printStackTrace();
}
read = true;
write = false;
}
}
}
控制台输出:
SQL * Plus:版本11.2.0.3.0 生产环境 于1月19日星期一14:07:53版权所有(c)1982,2011,Oracle . 版权所有 . 连接到:Oracle Database 11g企业版11.2.0.3.0版 - 64位 生产环境 使用分区,Oracle Label Security,OLAP,数据挖掘和实际应用程序测试选项SQL>
程序在读取BLINK_CHARACTER时挂起,我尝试了read()和readLine(),它没有读取BLINK_CHARACTER .
如果我做手动,在CMD提示中获得以下响应
C:> sqlplus USER / PASSWORD @ INSTANCE SQL * Plus:版本11.2.0.3.0 生产环境 于2015年1月19日星期一12:29:44版权所有(c)1982,2011,Oracle . 版权所有 . 连接到:Oracle Database 11g企业版11.2.0.3.0版 - 64位 生产环境 使用分区,Oracle Label Security,OLAP,数据挖掘和实际应用程序测试选项SQL> _ [BLINK_CHARACTER]
如何通过在运行时提供用户名和密码使其交互?我还需要为Informatica流程实现相同类型的交互式CMD提示 .
PS:在SQLPLUS中有单一命令本身登录和执行SQL文件的选项,但我想让它成为交互式的