如何使用java

时间:2017-04-27 06:52:03

标签: java sqlplus

我正在尝试从java运行sql脚本(memo_222dataFERG.out)并尝试从sqlplus窗口捕获输出。我的代码在这里

Runtime rt = Runtime.getRuntime();
                                String runString1 = "cmd /c start sqlplus pecok/pecok@xe @C:\workspace\PeCok_Tool\memo_222dataFERG.out";  
                                Process proc = null; 
                                proc = rt.exec(runString1);
                            //  proc = rt.exec("java -version");
                                InputStream is = proc.getErrorStream();
                                //InputStream is = proc.getInputStream();
                                int bufSize = 4096; 
                                BufferedReader in = new BufferedReader(new InputStreamReader(is), bufSize);
                                String currentLine = null; 
                                while ((currentLine = in.readLine()) != null) {
                                    System.out.println(" "  + currentLine); 
                                }
                                is.close();
                                int a = proc.waitFor();
                                System.out.println(" proc.waitFor() :: " +a);

Sqlplus执行。 SQLPlus窗口打开,它开始执行我在 C:\ workspace \ PeCok_Tool \ memo_222dataFERG.out 文件中编写的插入声明。但是我无法从sqlplus窗口捕获输出并在eclipse控制台中打印出来。 somewhare在while循环中挂起而不在eclipse控制台中打印任何内容。但是当我运行时“java -version”(在代码中注释)它在控制台中成功地将java版本打印为输出。现在我的问题是如何从sqlplus窗口读取输出?我尝试了getErrorStream()和getInputStream()。

1 个答案:

答案 0 :(得分:0)

我知道你是专门询问sqlplus和进程分叉但是SQLcl运行sqlplus所做的任何事情,并且完全用java编写,因此很容易集成。

我在这里有一个github repo,目标是:https://github.com/oracle/oracle-db-tools/tree/master/sqlcl/java

这是一个java示例:

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import oracle.dbtools.raptor.newscriptrunner.ScriptExecutor;
import oracle.dbtools.raptor.newscriptrunner.ScriptRunnerContext;

public class RunMyScript {

  public static void main(String[] args) throws SQLException, UnsupportedEncodingException {
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "klrice", "klrice");
    conn.setAutoCommit(false);

    // #create sqlcl
    ScriptExecutor sqlcl = new ScriptExecutor(conn);

    // #setup the context
    ScriptRunnerContext ctx = new ScriptRunnerContext();

    // #set the context
    sqlcl.setScriptRunnerContext(ctx);
    ctx.setBaseConnection(conn);

    // Capture the results without this it goes to STDOUT
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    BufferedOutputStream buf = new BufferedOutputStream(bout);
    sqlcl.setOut(buf);


    // # run a whole file 
    // adjust the path as it needs to be absolute
    sqlcl.setStmt("@/Users/klrice/workspace_commons/sqlcl-java/myfile.sql");
    sqlcl.run();


    String results = bout.toString("UTF8");
    results = results.replaceAll(" force_print\n", "");
    System.out.println(results);
  }

}