0

请看下面的代码。

private void printToConsole(List cellDataList) {
    for (int i = 1; i < cellDataList.size(); i++) {
        List cellTempList = (List) cellDataList.get(i);
        System.out.println(cellTempList);
    }
}

如何从上面的代码中获取数据以插入数据库。

String sql2 = "INSERT INTO Daily_Report (field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11,field12,field13,field14) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                        preparedstatement = con3.prepareStatement(sql2);
                        //Statement stmt = con3.createStatement();
                        System.out.println("1");
                        preparedstatement.setString(1, field1);
                        preparedstatement.setString(2, field2);
                        preparedstatement.setString(3, field3);
                        preparedstatement.setString(4, field4);
                        preparedstatement.setString(5, field5);
                        preparedstatement.setString(6, field6);
                        preparedstatement.setString(7, field7);
                        preparedstatement.setString(8, field8);
                        preparedstatement.setString(9, field9);
                        preparedstatement.setString(10, field10);
                        preparedstatement.setString(11, field11);
                        preparedstatement.setString(12, field12);
                        preparedstatement.setString(13, field13);
                        preparedstatement.setString(14, field14);
                        preparedstatement.executeUpdate(); 

您能否提一些建议...

4

3 回答 3

0
String sql2 = "INSERT INTO Daily_Report (field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11,field12,field13,field14) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
preparedstatement = con3.prepareStatement(sql2);

for (int i = 1; i < cellDataList.size(); i++) {
        List cellTempList = (List) cellDataList.get(i);
        for (int i = 0; i <14; i++) {
            preparedstatement.setString(i+1, cellTempList.size() > i ? cellTempList.get(i) : "");        
        }
}

假设 :

  1. cellDataList是您正在阅读的 excel 中所有行的列表。
  2. cellTempList是表示行内容的列表
于 2012-12-28T05:55:12.767 回答
0

就像是?

for (int i = 0; i < rowDataList.size() && i <14; i++) {
    preparedstatement.setString(i+1, rowDataList.get(i));        
}
for (int i = rowDataList.size()+1; i <=14; i++) {
    preparedstatement.setString(i+1, "");        
}
preparedstatement.executeUpdate();
于 2012-12-28T01:55:13.437 回答
0

写这个;

   for (int i = 0; i < cellDataList.size(); i++){
          stmt.executeUpdate ((String) cellDataList.get (i));
    }
于 2012-12-28T04:00:01.883 回答