I have to import a shapefile to PostgreSQL via a Java application. There are few codes that I've found and the code I've tried:
package shptodb;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class processbuilder {
public static void main( String[] args) {
ProcessBuilder pb= new ProcessBuilder();
pb.command("cmd.exe", "shp2pgsql -s 4326 C:\\Geodata\\Provinces.shp provinces | psql -U postgres -h localhost -p 5433 -d DB"
);
try {
Process process = pb.start();
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
int exitVal = process.waitFor();
if (exitVal == 0) {
System.out.println("Success!");
System.out.println(output);
System.exit(0);
} else {
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
I don't get any errors, so I don't know what's wrong with my code.
shp2pgsql
utility directly? Using the administrative login to load user data is an anti-pattern that can result in destruction of the database.