从sql plus在java中运行sql脚本

时间:2016-09-08 13:29:47

标签: java oracle sqlplus sql-scripts

我想使用sql plus运行sql脚本。问题是如果我的sql脚本中没有exit语句,则应用程序停止运行。我需要将exit与sql断开连接。我不想被删除,因为我想在数据库中维护其他查询的会话。请帮帮我

package pachet1;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javafx.scene.control.TextArea;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;

public class test1 {

    public static void main (String args []) {


    }

    public static void test_script (String scriptName, String alias, String path, TextArea txtArea) throws IOException {


        NtlmPasswordAuthentication userCred = new NtlmPasswordAuthentication("BCRWAN",
                "Sebastian.burchidrag", "Parola952491");
        SmbFile smbFile=new SmbFile(path  + scriptName, userCred);
        File file = new File("D://" + scriptName);
        try (InputStream in = smbFile.getInputStream()) {
            Files.copy(smbFile.getInputStream(), file.toPath());
        }
        String fileName = "@" + scriptName;
        String sqlPath = "D:\\";

        String sqlCmd = "sqlplus";

        String arg1   = "sys/sys@" +  alias + " " +  "as sysdba"; //plug in your user, password and db name
        String arg2   = fileName;
        try {
            String line;
            ProcessBuilder pb = new ProcessBuilder(sqlCmd, arg1, arg2);
            Map<String, String> env = pb.environment();
            env.put("VAR1", arg1);
            env.put("VAR2", arg2);
            pb.directory(new File(sqlPath));
            pb.redirectErrorStream(true);
            Process p = pb.start();
          BufferedReader bri = new BufferedReader
            (new InputStreamReader(p.getInputStream()));
          BufferedReader bre = new BufferedReader
            (new InputStreamReader(p.getErrorStream()));
          while ((line = bri.readLine()) != null) {
            txtArea.appendText(line + "\n");
          }
          bri.close();
          while ((line = bre.readLine()) != null) {
           txtArea.appendText(line + "\n");

          }
          bre.close();
          System.out.println("Done.");
        }
        catch (Exception err) {
          err.printStackTrace();
        }

    }

}

0 个答案:

没有答案