Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using QueryRunner to insert ArrayList<Object[]>

I want to use QueryRunner to perform an insert of an ArrayList. The only information I am finding online is for inserting one Object[]. Something along the lines of:

qr.update("insert into MyTable (param1,param2,param3) values (?,?,?)",
new Object[] { str1, str2, str3});

I would obviously like to avoid having to loop through an entire ArrayList and insert one index at a time due to the number of rows to be inserted being unknown each time.

I just wanted to see if anyone has done this. A query returns a List, so I do not see why I cannot insert a List. Any suggestions are appreciated. Thanks.

like image 836
Ken Avatar asked Dec 22 '22 11:12

Ken


1 Answers

I know this is old but I was looking for info about Apache Commons DBUtils QueryRunner and came across this... For future reference you can:

First convert the ArrayList into an Object[][]:

Object[][] params = null;
params = listOfObjectArrays.toArray(params);

Then pass params to the batch method on the QueryRunner (returns int[]):

qr.batch("insert into MyTable (param1,param2,param3) values (?,?,?)", params);
like image 153
Matt Avatar answered Jan 03 '23 06:01

Matt