各位高手,小弟有如下语句://插入大量数据 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").
         newInstance();
 conn = DriverManager.getConnection(str_url, str_user, str_password);
 
 //conn.setAutoCommit(false); //prepare for the commit

stmt = conn.createStatement();
   
int i = 0 ;
String[] sql = new String[list.size()];

//Just for insert
while (listit.hasNext()) 
{
OProduct op = (OProduct) listit.next() ;
sql[i] = " insert into t_product 。)" ;

stmt.executeUpdate(sql[i]);
i++ ;
} 在正常的情况是可以运行的,但是对于部份已插入的数据已存在 t_product 表中,由于主键的原因,导致程序抛出异常,但是我现在想他继续运行,把余下的记录都插入到表中,不知道有什么解决的方法?谢谢!Carlos

解决方案 »

  1.   

    1,用hibernate的merge
    2,用抓住异常,进行处理,这样就继续跑下去啦 
    try{
    stmt.executeUpdate(sql[i]); 
    }catch(Exception e){
    out.print("重复数据");
    }
    3,每次insert的时候,select数据,判断!
      

  2.   

    3,每次insert的时候,select数据,判断数据库中是否有重复!
      

  3.   

    如果不用 hibernate 呢?
      

  4.   

    try{
    stmt.executeUpdate(sql[i]);  ;
    }catch(Exception e){
    continue;
    }
      

  5.   

    To xblue3 :能具体一点吗?我是用SQL2000,你是指在SQL语句中加入SELECT语句检查记录是否存在?
    还是额外多一条SELECT语句检查?谢谢!!!
      

  6.   

    想到另一个办法 :sql[i] = " if exists (select pcode from t_product where pcode='"+op.pcode+"') begin insert into t_product (pcode,faccode,pdesc1,pdesc2,baseum,oldpcode,picture,phierarchy,matlgrp,gweight,nweight,weightum,volume,volumeum,crtdate,chgdate,ctrlactive)"+
     " values ( '"+op.pcode+"','"+op.faccode+"','"+op.pdesc1+"','"+op.pdesc2+"','"+op.baseum+"','"+op.oldpcode+"','"+op.picture+"','"+op.phierarchy+"','"+op.matlgrp+"',"+op.gweight+","+op.nweight+",'"+op.weightum+"',"+op.volume+",'"+op.volumeum+"',getdate()"+",getdate(),1 )"+
     " end " +
     " else " +
     " begin " +
     " update t_product set faccode = '"+op.faccode+"',pdesc1='"+op.pdesc1+"',pdesc2='"+op.pdesc2+"',baseum='"+op.baseum+"',oldpcode='"+op.oldpcode+"',picture='"+op.picture+"',phierarchy='"+op.phierarchy+"',matlgrp='"+op.matlgrp+"',gweight="+op.gweight+",nweight="+op.nweight+",weightum='"+op.weightum+"',volume="+op.volume+",volumneum='"+op.volumeum+"',chgdate=getdate()"+
     " where pcode = '"+op.pcode+"'" +
     " end" ;
    呵呵....本人比较笨....