Java – 通过读取CSV文件将记录插入SQL表中

huangapple 未分类评论37阅读模式
英文:

Java - Insert records into SQL table by reading a CSV file

问题

我不是高级的Java开发者。

我正在一个项目上工作,通过读取CSV文件中的数据将记录插入SQL数据库的表中。雇员CSV文件有多列包含数据。还有一个包含映射信息的XML文件,也就是说,XML文件指示CSV文件中的哪一列包含什么信息。

我已经成功地根据XML文件中的映射读取了CSV文件。我也成功地将CSV文件中的数据插入到数据库表中。但是有一个问题。CSV文件是一个包含所有员工历史记录的文件,按时间顺序排列(最早的记录在前)。在存在一个员工的多个记录的情况下,文件中的最后一条记录包含了他/她的当前信息,需要插入到Employee表中。他/她的所有旧记录需要按照它们在CSV文件中的顺序插入到Employee_History表中。CSV文件的第0列包含员工ID。以下是CSV文件的示例:

  1. Emp_ID|First Name|Last Name|Email|Update Date
  2. 123|John|Smith|john.smith01@email.com|2020-01-01
  3. 234|Bruce|Waye|bruce.wayne@wayneenterprises.com|2020-02-02
  4. 123|John|Smith|john.smith02@email.com|2020-02-15
  5. 345|Clark|Kent|clark.kent@dailyplanet.com|2020-02-16
  6. 123|John|Smith|john.smith03@email.com|2020-02-20 -- **Emp ID123CSV文件中的最后一条记录**

请问有人能告诉我最佳的方法来处理这个问题吗?我在努力想出一种方法来识别特定保管人在CSV文件中的最后一条记录。

英文:

I am not an advanced Java developer.

I am working on a project to insert records into tables in a SQL database by reading the data in a CSV file. The Employee CSV file has several columns containing data. There is an XML file which contains the mapping information, that is, the XML file says which column in the CSV file contains what information.

I have been successful in reading the CSV file with the mapping in the XML file. I have also been successful in inserting the data in the CSV file into database tables. But there is a catch. The CSV file is a file containing the all the historical records of employees in chronological order (oldest record first). In the scenario where there are multiple records for an employee, his/her last record in the file contains his/her current information and needs to be inserted into the Employee table. All of his/her older records need to be inserted into the Employee_History table in the same order they appear in the CSV file. Column 0 of the CSV file contains the Employee ID. The following is to give an idea of what the CSV file looks like

  1. Emp_ID|First Name|Last Name|Email|Update Date
  2. 123|John|Smith|john.smith01@email.com|01/01/2020
  3. 234|Bruce|Waye|bruce.wayne@wayneenterprises.com|02/02/2020
  4. 123|John|Smith|john.smith02@email.com|02/15/2020
  5. 345|Clark|Kent|clark.kent@dailyplanet.com|02/16/2020
  6. 123|John|Smith|john.smith03@email.com|02/20/2020 -- **Last record in the CSV file for Emp ID = 123**

Can anyone please tell me the best way to approach this? I am struggling to come up with a way to identify a given custodian's last record in the CSV file.

huangapple
  • 本文由 发表于 2020年4月5日 21:12:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/61043157.html
匿名

发表评论

匿名网友

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

确定