java 调用bat导入sql文件到postgresql

本文介绍了一种在SpringBoot应用中使用Logback进行日志管理的方法,通过自定义工具类实现数据库文件加载和批处理操作。文章详细展示了如何通过Java运行系统命令,批量执行SQL脚本,并将整个过程的日志输出到指定位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值