public static void importList2DB(List list) throws IOException {
 

 
 String sql="insert into ocs_feeitem(fee_id,description_text,product_line_descr,first_subject,cqd_cdr_type) values(";
 String sql2="insert into ocs_feeitem(fee_id,description_text,product_line_descr,first_subject,cqd_cdr_type) values(?,?,?,?,?)";
 PreparedStatement ps=null;
 Connection con =null;
 Statement stmt=null;
try{     
System.out.println("before con");
con=ConnectionFactory.getConnection();
 con.setAutoCommit(false);
 ps=con.prepareStatement(sql2);
 System.out.println("before stmt");
 stmt=con.createStatement();  
 for(int i=0;i<list.size();i++){
 System.out.println("---------------in for");
     TransferItem ti=(TransferItem)list.get(i);
sql=sql+ti.getFee_id()+",'"+ti.getDescription_text()+"','"+ti.getProduct_line_descr()+"','"+ti.getFirst_subject()+"',"+ti.getCqd_cdr_type()+")";
 
 /*ps.setInt(0, ti.getFee_id());
 ps.setString(0, ti.getDescription_text());
 ps.setString(1, ti.getProduct_line_descr());
 ps.setString(2, ti.getFirst_subject());
             ps.setInt(1, ti.getCqd_cdr_type());
 ps.executeUpdate();
             */
             stmt.executeUpdate(sql);  
     
 System.out.println("-------------");
 } 
 con.commit();
}catch(SQLException ex){

ex.printStackTrace();

}finally{
ConnectionFactory.close(con, ps, null);
}  
 
 
 
 
 }
 
 
 
 public static void main(String[] args) throws IOException{
 
List list=readExcel("D:\\bak\\transfer.xls");
//list中大概1000多条记录,但是执行第二条时报错无法插入
importList2DB(list);



 
 
 }

解决方案 »

  1.   

    不明白你为什么要创建2个Statement。既然用PreparedStatement为什么还要字符串拼接。总之你的程序好乱。
      

  2.   

    你这样做用两个Statement去持久化数据也许会违反数据库的唯一约束。当然第二条就会出错哦。
      

  3.   

    LZ建表的时候,有没有建约束,如唯一,主键,自增之类的
    应该是这方面出问题,调试可以看看SQL语句是否正确
      

  4.   

    con.setAutoCommit(false);
    应该是这个设置错了吧,如果用PreparedStatement对象这个好像可以。
    如果用Statement对象你最好把这个设置成true。
      

  5.   

    csdn昨天出问题!!!!你的问题昨天我回答过了。
    LZ要做的是批量更新吧?!批量更新 需要使用 PreparedStatement 来做,并且其语法应该是:String sql2="insert into ocs_feeitem(fee_id,description_text,product_line_descr,first_subject,cqd_cdr_type) values(?,?,?,?,?)"; ps=con.prepareStatement(sql2); 
    for(int i=0;i <list.size();i++){ //建议一次更新100条左右
      ps.setInt(1, ti.getFee_id()); //JDBC是从1开始的
      ...
      ps.setInt(1, ti.getCqd_cdr_type()); 
      ps.addBatch(); //和非指更新的特别之处
      //ps.clearParameters(); //可改善效屡 }ps.executeBatch();