简体   繁体   English

如何使用spring jdbctemplate将excel中的记录插入到数据库中

[英]How to insert records from excel to database using spring jdbctemplate

I need to insert data from excel to database which looks:我需要将数据从 excel 插入到如下所示的数据库中:

Id  Name    Phone   Joining Date  Subject
1   A      11111    14-Mar-2001   Cse
2   B      22222    25-Dec-2016   IT
3   C      33333    12-Dec-2011   ECE

If I have to perform batch insert in a single table then I am able to do it using spring jdbctemplate(batchUpdate(...)).如果我必须在单个表中执行批量插入,那么我可以使用 spring jdbctemplate(batchUpdate(...)) 来完成。

But I want it to insert data in multiple tables eg 1st 3 columns in Table1, next 2 in Table2, next n in table3 like this way.但我希望它像这样在多个表中插入数据,例如 Table1 中的第 1 3 列,Table2 中的下一个 2,table3 中的下一个 n。

For reading data I am using POI API and after extracting data m keeping it in List of Map object which looks:为了读取数据,我使用 POI API 并在提取数据后将其保存在 Map 对象列表中,如下所示:

allObj=[{0=1.0, 1=A, 2=11111.0, 3=2001-3-14 0:0:0, 4=Cse}, {0=2.0, 1=B, 2=22222.0, 3=2016-12-25 0:0:0, 4=IT}, {0=3.0, 1=C, 2=33333.0, 3=2011-12-12 0:0:0, 4=ECE}]

How to perform this tasks?如何执行此任务? not asking full solution but a hint.不是问完整的解决方案,而是一个提示。 Thanks谢谢

If coding is required then inform I am not posting it as it is lengthy and common.如果需要编码,请告知我不会发布它,因为它冗长且常见。

EDITED: Few didn't understand the Question!编辑:很少有人不明白这个问题!

I think u know batch update.我想你知道批量更新。 I am using JdbcTemplate of spring.我正在使用 spring 的 JdbcTemplate。 suppose I have table T1 as: Id|Name|Phone|Joining Date|假设我的表 T1 为:Id|Name|Phone|Joining Date| Subject in Database(using MYSQL)数据库中的主题(使用 MYSQL)

Now, I have an excel file with the corresponding values.I can read it and batch insert it into database by JdbcTemplate in that table.现在,我有一个带有相应值的 excel 文件。我可以读取它并通过该表中的 JdbcTemplate 将其批量插入到数据库中。

But Now I have two table as T1: Id|Name|Phone and T2: Joining Date|但是现在我有两个表作为 T1:Id|Name|Phone 和 T2:Joining Date| Subject主题

I have the same excel file.我有相同的excel文件。 NOW my question comes into the frame.现在我的问题进入框架。 How to insert the values in two tables?如何在两个表中插入值? If you get the question kindly remove your -ve vote.如果您收到问题,请删除您的 -ve 投票。

LOAD DATA LOCAL INFILE 
'C:\\temp\\file.csv'
INTO TABLE table_name
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(@col1,@col2)
set
column1 = @col1,
column2 = @col2;

Above query for table1, run same for other tables by changing column_names accordingly.上面对 table1 的查询,通过相应地更改 column_names 对其他表运行相同的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM