读书人

executeBatch有关问题

发布时间: 2012-03-12 12:45:33 作者: rapoo

executeBatch问题
我使用jdbc中的批量处理时,如果在commit时有一句sql出错,在catch异常时如何能定位到是那一条sql出的错误?

例如我批量插入,但是有一条记录违反主键约束,这时候返回异常,但是无法定位到底是哪条出错。

代码如下:
public String Commit(String sql[],int c) throws Exception{
String ret;
ret= "success ";
try{
m_conn.setAutoCommit(false);
for(int j=0;j <c;j++){
if(!sql[j].equals( " ")){
m_stmt.addBatch(sql[j]);
}
}
m_stmt.executeBatch();
m_conn.commit();
}
catch(Exception ex){
m_conn.rollback();
System.err.println(ex.getMessage());
ret=ex.getMessage();
}
return ret;
}

[解决办法]
you should look into SQLException information and trace the every record

读书人网 >J2SE开发

热点推荐