Correct way to use copy Postgres jdbc

前端 未结 2 1515
面向向阳花
面向向阳花 2020-12-31 23:27

Unable to use copy command with jdbc Postgres. Whats wrong with the below code snippet sample.

public boolean loadReportToDB(String date) {
        // TODO A         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-01 00:00

    This works for me:

    try (Connection conn = DriverManager.getConnection(connUrl, myUid, myPwd)) {
        long rowsInserted = new CopyManager((BaseConnection) conn)
                .copyIn(
                    "COPY table1 FROM STDIN (FORMAT csv, HEADER)", 
                    new BufferedReader(new FileReader("C:/Users/gord/Desktop/testdata.csv"))
                    );
        System.out.printf("%d row(s) inserted%n", rowsInserted);
    }
    

    Using copyIn(String sql, Reader from) has the advantage of avoiding issues where the PostgreSQL server process is unable to read the file directly, either because it lacks permissions (like reading files on my Desktop) or because the file is not local to the machine where the PostgreSQL server is running.

提交回复
热议问题