本文介绍了如何使用Java ProcessBuilder从CMD提示符执行SQL文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用ProcessBuilder编写了一个Java程序来启动CMD提示&连接SQLPLUS以执行一些SQL文件.

I have written a java program using ProcessBuilder to launch CMD prompt & connect SQLPLUS to execute some SQL files.

public class OracleConnect {

    public static void main(String[] args) throws IOException {
        String[] cmd = new String[] { "sqlplus", "<USER>/<PASSWORD>@<INSTANCE>" };
        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;
        }
    }
}

控制台中的输出:

(c)1982,2011,Oracle版权所有.保留所有权利.

Copyright (c) 1982, 2011, Oracle. All rights reserved.

已连接到:Oracle Database 11g企业版发行版 11.2.0.3.0-具有分区,Oracle标签安全性,OLAP,数据挖掘和实际应用程序测试选项的64位生产

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production With the Partitioning, Oracle Label Security, OLAP, Data Mining and Real Application Testing options

SQL>

程序在读取BLINK_CHARACTER时挂起,我同时尝试了read()和readLine(),但它没有读取BLINK_CHARACTER.

The program hangs on reading the BLINK_CHARACTER, I tried both read() and readLine() it doesn not read BLINK_CHARACTER.

如果我做手动,在CMD提示符下得到以下响应

If I do manual, getting following response in CMD prompt

SQL * Plus:2015年1月19日星期一发布11.2.0.3.0版

SQL*Plus: Release 11.2.0.3.0 Production on Mon Jan 19 12:29:44 2015

(c)1982,2011,Oracle版权所有.保留所有权利.

Copyright (c) 1982, 2011, Oracle. All rights reserved.

已连接到:Oracle Database 11g企业版发行版 11.2.0.3.0-具有分区,Oracle标签安全性,OLAP,数据挖掘和实际应用程序测试选项的64位生产

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production With the Partitioning, Oracle Label Security, OLAP, Data Mining and Real Application Testing options

SQL> _ [BLINK_CHARACTER]

SQL> _[BLINK_CHARACTER]

如何通过在运行时提供用户名和密码来使其具有交互性?而且我还需要为Informatica流程实现相同类型的交互式CMD提示.

How to make it interactive by giving username and password at runtime? and also i need to implement the same kind interactive CMD prompt for Informatica process also.

PS:有用于登录&的选项在SQLPLUS中以单个命令本身执行SQL文件,但我想使其具有交互性

PS : there are options to login & execute SQL files in a single command itself in SQLPLUS but i want to make it interactive

推荐答案

如果从连接字符串中删除用户名/密码和@字符,SQL * Plus将提示输入用户名/密码:

If you remove the username/password and the @ character from your connect string, SQL*Plus will prompt for the username/password:

sqlplus <INSTANCE>

这篇关于如何使用Java ProcessBuilder从CMD提示符执行SQL文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 04:00