1. 程式人生 > >java 呼叫bat匯入sql檔案到postgresql

java 呼叫bat匯入sql檔案到postgresql

package com.example.springbootlogback.utils;

import java.io.File;
import java.io.IOException;

public class CommandUtil {

    public static void execute(String filePath, String fileName) throws IOException, InterruptedException {
        Process process  = Runtime.getRuntime().exec("cmd /c start /wait "+filePath+File.separator+fileName);
        process.waitFor();
        process.destroy();
    }

    public static void executeBg(String filePath, String fileName) throws IOException, InterruptedException {
        Process process  = Runtime.getRuntime().exec("cmd /c start /b /wait "+filePath+File.separator+fileName);
        process.waitFor();
        process.destroy();
    }

    public static String buildImportSql(String pgHome, String host, String port, String dbName, String username, String password, String dir,  String fileName){
        return pgHome+"/bin/psql -h "+host+" -p "+port+" -U "+username+" -d "+dbName+" <"+dir+ File.separator+fileName +" >"+dir+ File.separator+fileName+".log";
    }

}
package com.example.springbootlogback.utils;

import java.io.*;
import java.util.List;

public class FileUtil {

    public static void writeFile(String filePath, String fileName, List<String> command) throws IOException {

        File file = new File(filePath+File.separator+fileName);

        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));

        for(String ss : command){
            bufferedOutputStream.write((ss + "\r\n").getBytes());
        }
        bufferedOutputStream.flush();
        bufferedOutputStream.close();

    }

}
package com.example.springbootlogback.controller;

import com.example.springbootlogback.utils.CommandUtil;
import com.example.springbootlogback.utils.FileUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FileUploadController {

    @RequestMapping("/loadData")
    public String loadDatafromDbfile() throws IOException, InterruptedException {

        String pgHome = System.getenv("PG_HOME");

        String host = "localhost";
        String port = "5432";
        String dbName = "test";
        String username = "postgres";
        String password = "postgres";
        String dir = "E:\\testdata";
        String fileName = "t_point.sql";

        List<String> list  = new ArrayList<>();
        String command1 = "SET PGPASSWORD="+password;
        String command2 = CommandUtil.buildImportSql(pgHome, host, port,dbName,username, password, dir, fileName);
        System.out.println(command2);
        list.add(command1);
        list.add(command2);
        list.add("exit");
        FileUtil.writeFile("E:\\testdata","test.bat",list);
        CommandUtil.execute("E:\\testdata","test.bat");

        return "true";
    }

    @RequestMapping("/loadDataBg")
    public String loadDatafromDbfile1() throws IOException, InterruptedException {

        String pgHome = System.getenv("PG_HOME");

        String host = "localhost";
        String port = "5432";
        String dbName = "test";
        String username = "postgres";
        String password = "postgres";
        String dir = "E:\\testdata";
        String fileName = "t_point.sql";

        List<String> list  = new ArrayList<>();
        String command1 = "SET PGPASSWORD="+password;
        String command2 = CommandUtil.buildImportSql(pgHome, host, port,dbName,username, password, dir, fileName);
        System.out.println(command2);
        list.add(command1);
        list.add(command2);
        FileUtil.writeFile("E:\\testdata","test.bat",list);
        CommandUtil.executeBg("E:\\testdata","test.bat");

        return "true";
    }


}