java - 如何在同一个查询中同时将两个Arraylist值插入到数据库表中

标签 java loops insert arraylist

我有两个必须插入到数据库中的 ArrayList。我有用于在数据库中插入一个 arraylist 值的代码...这是我的第一个用于在数据库中插入值的 arraylist

for (int j = 0; j < list.size(); j++) {
    int d = (int) list.get(j);
    stmt.executeUpdate("insert into cdrcost  (calldate) value ('" + d+ "'));
}

现在,根据我的需要,我有另一个数组列表可以在我在这里提到的同一查询中插入到数据库中。所以我需要任何路径,以便将这两个数组列表的值插入到数据库中。 任何帮助将不胜感激... 提前感谢...

最佳答案

PreparedStatement psth = dbh.prepareStatement("insert into cdrcost  (calldate) value (?)");
for (List<Integer> lst: Arrays.<List<Integer>>asList(list1,list2))
  for (int value: lst) {
    psth.setInt(1,value);
    psth.addBatch();
  }
psth.executeBatch();

如果您需要设置超过 1 个值:

PreparedStatement psth = dbh.prepareStatement("insert into cdrcost  (calldate, othercolumn) value (?, ?)");
Iterator<Integer> it1 = list1.iterator();
Iterator<Integer> it2 = list2.iterator();
for (; it1.hasNext() && it2.hashNext();) {
  psth.setInt(1,it1.next());
  psth.setInt(2,it2.next());
  psth.addBatch();
}
psth.executeBatch();

关于java - 如何在同一个查询中同时将两个Arraylist值插入到数据库表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12836869/

相关文章:

Java 7 使用可变参数重载

java - PayPal IPN 在实时返回 INVALID 并在沙盒上返回 VERIFIED

java - 如何在java中读取文件时标记位置?

Javascript:按键求和多个数组的最有效方法

如果存在具有 2 个特定列的行,则 MYSQL 更新表;如果不存在,则插入新行

.net - 如何使用 Linq-to-SQL 只插入新记录?

java - 以编程方式在android Edittext中获取提示文本值

python - 使用 BeautifulSoup 循环遍历表行

c - 使用递归打印控制台 "picture"

php - MySQL (PDO) exec 不返回任何内容,没有错误,没有添加行