简体   繁体   中英

insert values of arraylist in to database

i have 4 arraylists i want to add the values of lists in database

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

now i want to add ordername1 in the 1st row of ordername column and ordername2 in 2nd row of ordername column and so on for other lists.

there will be 4 columns

each item will insert in each row.

and some variables

like special instruction, table no and waiter name they will also inserted in database along with lists

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

This should help:

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));
        }
    }

}

i have solved my problem myself, the code is

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");
        }


    }

this code works for entering 4 lists in to database,

Note: my all lists size are equal so thats why i have iterated all lists in a single for loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM