How to execute SQL file from CMD prompt using Java ProcessBuilder

感情迁移 提交于 2020-01-14 19:12:36

问题


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;
        }
    }
}

Output in Console :

SQL*Plus: Release 11.2.0.3.0 Production on Mon Jan 19 14:07:53 2015

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

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>

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

If I do manual, getting following response in CMD prompt

C:>sqlplus USER/PASSWORD@INSTANCE

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

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

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]

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 : there are options to login & execute SQL files in a single command itself in SQLPLUS but i want to make it interactive


回答1:


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

sqlplus <INSTANCE>

SQL*Plus: Release 11.2.0.0.0 - Production on Mon Jan 19 14:07:53 2015
(c) Copyright 1982, 2008 Oracle Corporation. All rights reserved.
Enter user-name:



来源:https://stackoverflow.com/questions/28020813/how-to-execute-sql-file-from-cmd-prompt-using-java-processbuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!