将arraylist的值插入数据库

时间:2016-07-03 09:34:16

标签: java mysql arraylist

我有4 arraylists我想在数据库中添加列表值

ordername="ordername1, ordername2";
orderprice="orderprice1, orderprice2";
ordertime="ordertime1, ordertime2";
orderquantity="orderquantity1, orderquantity2";

现在我想在ordername1列的第一行添加ordernameordername2列的第二行{和ordername,依此类推其他列表。

将有4列

每个项目都会插入每一行。

和一些变量

像特殊指令,表格号和服务员名称一样,它们也将与列表一起插入数据库

------------------------------------------------------------------------------
  |ordername | orderprice | ordertime | orderquantity | specinst |tableNo| wname
  ------------------------------------------------------------------------------
  |ordername1| orderprice1|ordertime1 |orderquantity1 | spicy    |  1    | john|
  |ordername2| orderprice2|ordertime2 |orderquantity2 | not spicy| 2     |alice|
  ------------------------------------------------------------------------------

2 个答案:

答案 0 :(得分:0)

这应该有所帮助:

import java.util.*;
import static java.util.Arrays.asList;

public class Zipper {
    public static Iterator<Object[]> zip(Iterable<? extends Object> first, Iterable<? extends Object>...rest) {
        final Iterator<? extends Object> firstIterator = first.iterator();
        final List<Iterator<? extends Object>> restIterators = extractIterators(rest);

        return new Iterator<Object[]>() {
            public boolean hasNext() {
                return firstIterator.hasNext();
            }

            public Object[] next() {
                List<Object> result = new ArrayList<Object>(restIterators.size() + 1);
                result.add(firstIterator.next());
                result.addAll(collectResultsFromRest(restIterators));
                return result.toArray();
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

    private static Collection<Object> collectResultsFromRest(List<Iterator<?>> iterators) {
        List<Object> result = new ArrayList<Object>();

        for (Iterator<?> iter : iterators)
            if (iter.hasNext()) result.add(iter.next());
            else result.add(null);

        return result;
    }

    private static List<Iterator<? extends Object>> extractIterators(Iterable<?>[] rest) {
        List<Iterator<? extends Object>> result = new ArrayList<Iterator<? extends Object>>(rest.length);
        for (Iterable<?> it : rest)
            result.add(it.iterator());

        return result;
    }

    public static void main(String[] args) {
        List<String> ordername = asList("ordername1, ordername2".split(",\\s+"));
        List<String> orderprice= asList("orderprice1, orderprice2".split(",\\s+"));
        List<String> ordertime= asList("ordertime1, ordertime2".split(",\\s+"));
        List<String> orderquantity=asList("orderquantity1, orderquantity2".split(",\\s+"));


        Iterator<Object[]> zip = Zipper.zip(ordername, orderprice, ordertime, orderquantity);

        while (zip.hasNext()) {
            Object[] row = zip.next();
            // here you add code for saving to database
            // for this example we are just printing results to console
            System.out.println(Arrays.toString(row));
        }
    }

}

答案 1 :(得分:0)

我自己解决了我的问题,代码是

for (int i=0; i<nameList.size() && i<priceList.size() && i<timeList.size() && i<quantityList.size(); i++){

        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(url, uname, pass);

        stmt = con.createStatement();
        String sql = "insert into currentorders (ordername, orderprice, ordertime, orderquantity, specInst, tableNo, wname) VALUES ('"+nameList.get(i)+"', '"+priceList.get(i)+"', '"+timeList.get(i)+"','"+quantityList.get(i)+"','"+specialInst+"','"+tableNo+"','"+waitername+"')";

        int count = stmt.executeUpdate(sql);
           if(count == 1){

    System.out.println("enteredd");
    //else return "not entered";
        }
        else {
    System.out.println("not enteredd");
        }


    }

此代码适用于在数据库中输入4个列表,

  

注意:我的所有列表大小都相等,这就是为什么我在一个for循环中迭代所有列表