Newer
Older
osmCoverage / src / osm / jp / postgis / ExportCSV.java
@hayashi hayashi on 7 Jul 2018 1 KB ToPostgisの構造を大改造
package osm.jp.postgis;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import jp.co.areaweb.tools.database.DatabaseTool;

public class ExportCSV {
    public static String tableName;
    public static String CSV_FILE_NAME = "t_busstop.csv";
    
    public static void main (String[] argv) {
        tableName = "t_FUEL";
        CSV_FILE_NAME = tableName +".csv";
        try {
            outputDb();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void outputDb() throws Exception {
        File csvFile = new File(CSV_FILE_NAME);
        try (BufferedWriter ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8"));
            Connection con = DatabaseTool.openDb("postgis")) 
        {
            // CSV header
            System.out.println("gid,geom,score");
            ow.write("gid,geom,score");
            ow.newLine();
            PreparedStatement ps8 = con.prepareStatement("SELECT gid,idref,fixed,area,geom FROM t_busstop");
            try (ResultSet rset8 = ps8.executeQuery()) {
                while (rset8.next()) {
                    String gid = rset8.getString("gid");
                    String geom = rset8.getString("geom");
                    int score = rset8.getInt("fixed");
                    
                    String osm_node;
                    osm_node = ""+ gid +","+ geom +","+ score +"";
                    System.out.println(osm_node);
                    ow.write(osm_node);
                    ow.newLine();
                }
            }
            ow.flush();
        }
    }
}