各位高手,小弟有如下语句://插入大量数据 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
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
2,用抓住异常,进行处理,这样就继续跑下去啦
try{
stmt.executeUpdate(sql[i]);
}catch(Exception e){
out.print("重复数据");
}
3,每次insert的时候,select数据,判断!
stmt.executeUpdate(sql[i]); ;
}catch(Exception e){
continue;
}
还是额外多一条SELECT语句检查?谢谢!!!
" 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" ;
呵呵....本人比较笨....