gpt4 book ai didi

java - 如何从java中的列表返回变量并进入mysql数据库?

转载 作者:行者123 更新时间:2023-11-29 13:33:43 28 4
gpt4 key购买 nike

我想返回所有的数组列表,但只有一个被返回,不明白为什么会发生这种情况?花了一整天的时间试图了解这种错误的原因。

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]

我想返回所有的ArrayList。

此外,我想根据第一列元素的值将第一列的元素与第二列和第三列的元素按行分组在一起,以便进入 mysql 数据库。这里第一列是主 id ,基于此我们必须输入值,例如 ego kit1-->silicone dolls for sale,samsung 等。

我不想mysql入门的步骤,只想根据上面的元素创建Sql语句。

刚接触Java,现在卡住了,请帮忙。

最佳答案

for (int i=0; i<build.size();){
System.out.println("Element :"+build.get(i));
return build.get(i);
}

这个for循环返回 first element在ArrayList中找到build .

I want return all the ArrayList.

因为您想要返回 build 中存在的所有元素列表(它们本身就是 ArrayList),您需要更改 return type您的main方法:

public List<List<String>> main(){

并返回build列表如下:

for (int i=0; i<build.size();){
System.out.println("Element :"+build.get(i));
//return build.get(i);
}
return build;

关于java - 如何从java中的列表返回变量并进入mysql数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19062233/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com