2

I have a csv file which I want to import to table in postgres

The table contains 3 fields (id text, name text, geo geometry). The csv file is in the same format (3 values separate with comma).

I want to use java code in order to import the file (input.csv) into the table (tbl)

How can I do it ? Is there a query which I pass the file path to the DB ?

Boom
  • 1,145
  • 18
  • 44
  • "Is there a query which I pass the file path to the DB" - will the file be accessible from the database server (by the PostgreSQL user; the user ID the server runs as); If so you could use [COPY](https://www.postgresql.org/docs/12/sql-copy.html). – Brits Aug 27 '20 at 05:23
  • 1
    You could use the CopyManager API. See [here](https://stackoverflow.com/questions/6958965) or [here](https://stackoverflow.com/questions/18608144) –  Aug 27 '20 at 05:30

1 Answers1

1

You can use OpenCSV to read csv file into java object (here is an example - https://www.geeksforgeeks.org/mapping-csv-to-javabeans-using-opencsv/), and then use Java JPA Repository to insert data into database (here is an example - https://www.baeldung.com/spring-data-crud-repository-save)