在Java中从cmd运行sql脚本的问题

时间:2016-09-09 07:38:15

标签: java sql oracle cmd

我在一个执行sql脚本的应用程序上工作。文件由共享文件夹中的某人加载。我的想法是,如果脚本的主体不包含exit语句,我的应用程序就会停止。我不想退出,因为我需要为用户提供按提交或回滚的可能性,然后退出。

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

    }

}

这是我的脚本正文:

Prompt drop TABLE ANOTHER_TEST;
DROP TABLE ANOTHER_TEST CASCADE CONSTRAINTS
/

Prompt Table ANOTHER_TEST;
CREATE TABLE ANOTHER_TEST
(
  BATCH_SEQ             NUMBER,
  BATCH_GROUP_ID        NUMBER,
  STATUS_FLAG           VARCHAR2(30 BYTE),
  OBJ_BEING_PROCESSED   VARCHAR2(80 BYTE),
  BATCH_RUN_START_DTTM  DATE,
  BATCH_RUN_END_DTTM    DATE,
  CREATE_DTTM           DATE,
  CREATE_USER           VARCHAR2(30 BYTE),
  UPDATE_DTTM           DATE,
  UPDATE_USER           VARCHAR2(30 BYTE)
)
LOGGING 
NOCOMPRESS 
NOCACHE
PARALLEL ( DEGREE DEFAULT INSTANCES DEFAULT )
MONITORING
/

COMMENT ON TABLE ANOTHER_TEST IS 'This is a test table.'
/

EXIT
/

0 个答案:

没有答案