3

我有两个数组列表插入到数据库表的 2 列中,如下所示:

arraylist1: 123444, 324555, 6423643, 532326
arraylist2: jkfdsl, nkjfsdlj, jdslkfjdlkj, jfsldjfsk, fjdlskjfs

我编写了以下代码来插入数组列表,但它不起作用。我会感谢你的帮助。

try {
// Prepare a statement to insert a record

String sql = "INSERT INTO soundsdata.splog (arraylist1, arraylist2) VALUES(?,?)";
pstmt = (PreparedStatement) con.prepareStatement(sql);

pstmt.setArray(1,sptospring);
pstmt.setString(2,eachList.toString());

// Insert the row
pstmt.executeUpdate();
}finally {
pstmt.close();
}
4

5 回答 5

5

您可以执行以下操作:

假设您正在尝试创建一行,其中第一column行将包含第一行的内容,ArrayList以逗号分隔格式,第二column行将包含第二行的内容ArrayList

StringBuilder buffer = new StringBuilder();
boolean processedFirst = false;
String firstParam = null, secondParam = null;

try{
    for(String record: arrayList1){
        if(processedFirst)
            buffer.append(",");
        buffer.append(record);
        processedFirst = true;
    }
    firstParam = buffer.toString();
}finally{
    buffer = null;
}
processedFirst = false;
buffer = new StringBuilder();
try{
    for(String record: arrayList2){
        if(processedFirst)
            buffer.append(",");
        buffer.append(record);
        processedFirst = true;
    }
    secondParam = buffer.toString();
}finally{
    buffer = null;
}
secondParam = buffer.toString();

String sql = "INSERT INTO soundsdata.splog (arraylist1, arraylist2) VALUES(?,?)";
try{
    psmt = (PreparedStatement) con.prepareStatement(sql);
    pstmt.setString(1,firstParam);
    pstmt.setString(2,secondParam);
    pstmt.executeUpdate();
}finally {
    pstmt.close();
}
于 2012-08-26T04:08:29.103 回答
2

您不能将 ArrayList 存储在 varchar 列中。

您需要存储一个字符串。

于 2012-08-26T03:36:13.773 回答
1
PreparedStatement ps = connection.prepareStatement(query);            
for (Record record : arraylist1) {
    int index=1;                        
    ps.setString(index++,record.getItem());
    ps.setString(index++,record.getItem2());
    //
}
ps.executeBatch();
conn.commit(); 
于 2012-08-26T03:46:04.943 回答
0

插入多条记录:

public String saveOrder(ArrayList<KIT0053MBean> 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<KIT0053MBean> 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);             
    }
}
于 2012-08-26T05:07:18.313 回答
-2

String[] stringArray = lists.toArray(new String[lists.size()]);

            String string1 = stringArray[0];
            String string2 = stringArray[1];
            String string3 = stringArray[2];
            String string4 = stringArray[3];
            String string5 = stringArray[4];

// 然后写查询插入到数据库

插入表名值(string1 ......)

于 2016-01-14T11:24:46.723 回答