简体   繁体   中英

How to insert records from excel to database using spring jdbctemplate

I need to insert data from excel to database which looks:

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(...)).

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.

For reading data I am using POI API and after extracting data m keeping it in List of Map object which looks:

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. suppose I have table T1 as: Id|Name|Phone|Joining Date| Subject in Database(using 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.

But Now I have two table as T1: Id|Name|Phone and T2: Joining Date| Subject

I have the same excel file. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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