如何通过Java代码将CSV文件数据导入PostgreSQL表格?

huangapple go评论77阅读模式
英文:

How to import CSV file data into a PostgreSQL table via java code?

问题

我有一个 csv 文件,我想要将其导入到 postgres 数据库的表中。

该表包含 3 个字段(id 文本,name 文本,geo 几何)。
csv 文件的格式与此相同(3个值由逗号分隔)。

我想要使用 Java 代码将文件(input.csv)导入到表(tbl)中。

我应该如何实现呢?
是否有一个查询,我可以将文件路径传递给数据库?

英文:

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 ?

答案1

得分: 1

你可以使用OpenCSV将CSV文件读取到Java对象中(这里有一个示例 - https://www.geeksforgeeks.org/mapping-csv-to-javabeans-using-opencsv/),然后使用Java JPA Repository将数据插入数据库(这里有一个示例 - https://www.baeldung.com/spring-data-crud-repository-save)。

英文:

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)

huangapple
  • 本文由 发表于 2020年8月27日 13:14:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63609599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定