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


}
Java中将Shapefile(.shp)数据导入PostgreSQL数据库,通常需要借助一些第三方库如JTS Topology Suite(处理几何数据)和PostGIS(PostgreSQL的空间扩展)。以下是简单的步骤: 1. **添加依赖**: - 首先,你需要在项目中添加JTS和PostGIS的依赖。如果你使用Maven,可以在`pom.xml`中加入: ```xml <dependency> <groupId>org.locationtech.jts</groupId> <artifactId>jts-core</artifactId> <version>1.18.0</version> </dependency> <dependency> <groupId>org.postgis</groupId> <artifactId>postgis-jdbc</artifactId> <version>2.4.6</version> </dependency> ``` 2. **读取Shapefile**: 使用JTS库可以读取Shapefile数据,例如: ```java ShapeFileReader reader = new ShapeFileReader(new File("path_to_your_shp_file.shp")); GeometryFactory geometryFactory = new GeometryFactory(); List<Feature> features = reader.getFeatures(geometryFactory); ``` 3. **转换为PostGIS能理解的数据结构**: 将JTS的Geometry对象转换为PostGIS的WKB(Well-Known Binary)格式。 4. **插入到数据库**: 使用PostGIS JDBC驱动,创建连接并执行SQL插入语句: ```java Connection conn = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/your_database", "username", "password" ); PreparedStatement stmt = conn.prepareStatement( "INSERT INTO your_table (geom_column) VALUES (%s)" ); for (Feature feature : features) { WkbPoint geom = (WkbPoint) feature.getDefaultGeometry().getGeometry(); stmt.setBytes(1, geom.wkb()); stmt.execute(); } conn.close(); ``` 5. **注意错误处理**: 在实际操作中,记得处理可能出现的文件加载错误、数据库连接错误以及数据插入失败等异常情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值