Reputation: 1176
I am extracting data using some xyz logic. After extraction I am getting multiple list in each iteration.
for (int i= 0; i<= 5; i++)
{
for (int j= 0; j<= 5; j++)
{
//data extraction logic
lList1.add(value1);
lList2.add(value2);
lList3.add(value3);
}
//in each iteration I am getting list different
System.out.println(" lList1 for iteration "+i+"is: "+lList1);
System.out.println(" lList2 for iteration "+i+"is: "+lList2);
System.out.println(" lList3 for iteration "+i+"is: "+lList3);
}
I need to pass these list to database. Each list is associated with one column in db.
Example: lList1 is for column1, lList2 is for column2, lList3 is for column3 etc
What is best way to pass these list to db or to write these values row by row in java output should be like
row 1 lList1 lList2 lList3 etc of first iteration list values
row 2 lList1 lList2 lList3 etc of second iteration list values etc
Can anyone help?
Upvotes: 1
Views: 2240
Reputation: 49
if all list contains same length then make one common function for all ArrayList and iterate it with the loop of the maximum size ArrayList:
It should be like:
for(int i=0;i<lList1.size();i++) {
String insertQueryMainProduct = "INSERT INTO " + "" + tableName + "" + "(ColumnOneName,ColumnSecondName,ColumnThirdName)" + " VALUES('" +lList1.get(i)+ "', '" + lList2.get(i)+ "', '" + lList3.get(i)+ "')";
System.out.println("Query: " + insertQueryMainProduct);
statement.executeUpdate(insertQueryMainProduct);
Hope this will help you
Upvotes: 0
Reputation: 26
Create a class which will contain three members col1,col2 and col3 and make list of that class and use it.
class MyRow
{
public {col1datatype} col1;
public {col2DataType} col2;
public {col3DataType} col3;
}
your function
List<MyRow> lstRows = new ArrayList<MyRow>();
for (int i= 0; i<= 5; i++)
{
for (int j= 0; j<= 5; j++)
{
MyRow row = new MyRow();
row.col1 = value1;
row.col2 = value2;
row.col3 = value3;
llstRows.Add(row);
}
}
And use that lstRows accordingly at database transaction.
Upvotes: 1
Reputation: 1332
You can use Multidimensional Collections
ArrayList<ArrayList<Object>> a = new ArrayList<ArrayList<Object>>();
Result can be like that (based on your logic):
Multidimensional ArrayList: [[3, 4], [12, 13], [22, 23], [33,23]]
here is a full example :
ArrayList<ArrayList<Object>> list = new ArrayList<ArrayList<Object>>();
for (int i= 0; i<= 5; i++) {
//if you got all the values
list.add(new ArrayList<Object>(Arrays.asList(value1, value2, value3)));
//else you have to fetch them from another loop
for (int j= 0; j<= 5; j++) {
list.add(new ArrayList<Object>());
list.get(j).add(value1);
}
}
Upvotes: 1
Reputation: 1103
I would suggest in that case to serialize it to Json and store it in the column (since you use this approach with columns for lists).
When retrieving data from the DB, you can just deserialize it from Json to a List object.
For example Entity Framework is doing the same in .NET
Upvotes: 0
Reputation: 12277
I would suggest the following flow:
This way you will have each row in DB, the same way you need to list them out later in your front end. Also it will be easier to do any data search.
Upvotes: 0