Write arraylist to a database java

前端 未结 5 1702
有刺的猬
有刺的猬 2020-12-21 06:22

I have two arraylists to insert into 2 columns in a database table as follows:

arraylist1: 123444, 324555, 6423643, 532326
arraylist2: jkfdsl, nkjfsdlj, jdsl         


        
5条回答
  •  没有蜡笔的小新
    2020-12-21 06:52

    Insert more than one record:

    public String saveOrder(ArrayList insertList){     
        System.out.println("SaveOrder DAO  Method is calling " +insertList.size());
        Connection con=null;
        PreparedStatement ps2=null;
        try {
            con=open();
            con.setAutoCommit(false);
            con.getTransactionIsolation();
            ps2=con.prepareStatement(sql1);             
            Iterator it=insertList.iterator();            
            while(it.hasNext()){
                KIT0053MBean order=(KIT0053MBean)it.next();                 
                ps2.setString(1, model.getCustomerid());
                ps2.setString(2, model.getSerialid());                  
                ps2.addBatch();
            }               
            int i[]=ps2.executeBatch();
            System.out.println("###### insertion1### row   "+i.length);    
            con.commit();
            con.setAutoCommit(true);                
        } catch (Exception e) 
        {               
            System.out.println(e.getMessage());     
        }finally{
            close(con);         
            close(ps2);             
        }
    }
    

提交回复
热议问题