我有一个字符串数组列表
ArrayList myList = new ArrayList();
myList = [url1,url2,url3];
我需要将这些网址插入数据库中的 3 个不同行中。这就是我的做法。
while(myList.size()!=0)
{
//get individual values in the array list
int idx=0;
String url= myList.get(idx++).toString() ;
String insert="INSERT into test (url) values (?)";
prepstmt = conn.prepareStatement(insert);
prepstmt .setString(r++, url);
prepstmt.executeUpdate();
}
这会进入无限循环。 有人可以帮我纠正我的代码吗?插入部分代码就可以了。但我无法获取各个网址。
谢谢!
最佳答案
试试这个:
String insert = "INSERT into test (url) values (?)";
for (String url : myList) {
prepstmt = conn.prepareStatement(insert);
prepstmt.setString(1, url);
prepstmt.executeUpdate();
}
关于java - 在表中插入数组列表的各个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15710120/