Friday, 27 September 2013

How to Return variables from a List in java and entry into mysqldatabase?

How to Return variables from a List in java and entry into mysqldatabase?

I want to return all the arraylist but only one is getting returned, do
not understand why such thing is happening?Spent the whole day trying to
understand the reason for such error.
Code : -
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import au.com.bytecode.opencsv.CSVReader;
public class CSVFileReader {
public List<String> main() {
String startFile = "/Users/sample.csv";
List<List<String>> build = new ArrayList<List<String>>();
List<String> tempArr = null;
try {
CSVReader reader = new CSVReader(new FileReader(startFile));
String[] line = null;
String[] headers = reader.readNext();
// generate headers
for(String header : headers){
tempArr = new ArrayList<String>();
// tempArr.add(header);
build.add(tempArr);
}
// generate content
while((line = reader.readNext())!=null){
for (int i = 0; i < build.size(); i++) {
tempArr = build.get(i);
String val = line[i];
tempArr.add(val);
//System.out.println("the value of the array variable is
:"+val);
build.set(i, tempArr);
}
}
for(List<String> string:build){
System.out.println("The value of the array and their
variables are :"+string);
}
for (int i=0; i<build.size();){
System.out.println("Element :"+build.get(i));
return build.get(i);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String[] args){
CSVFileReader csvfilereader = new CSVFileReader();
System.out.println("the returned variable is :"+csvfilereader.main());
}
}
The CSV file is like:(Could Be A file of any size and any number of columns)
Keyword,AlternateKeyword,PrintValue
ego kit1,silicone baby dolls for sale,samsung
ego kit2,ego ce4,samsung
ego kit3,venus,samsung,
ego kit4,zz cream,samsung
ego kit5,samsung galaxy 7.7 case,samsung
ego kit6,apple,samsung
And the Output is :
The value of the array and their variables are :[ego kit, ego kit, ego
kit, ego kit, ego kit, ego kit]
The value of the array and their variables are :[silicone baby dolls for
sale, ego ce4, venus, zz cream, samsung galaxy 7.7 case, apple]
The value of the array and their variables are :[Samsung, Samsung,
Samsung, Samsung, Samsung, Samsung]
Element :[ego kit1, ego kit2, ego kit3, ego kit4, ego kit5, ego kit6]
the returned variable is :[ego kit, ego kit, ego kit, ego kit, ego kit,
ego kit]
I want return all the ArrayList.
Also I want to group together the elements of the first column with the
elements of second and third column row-wise for entry into a
mysqldatabase, based on the value of the first column elements.Here the
first column is the primary id, based on which we have to enter the
values, like for ego kit1-->silicone dolls for sale,samsung and likewise.
I do not want the steps for mysql entry just want to create the Sql
statement based on the elements above.
New to Java, stuck at this point, please help.

No comments:

Post a Comment