日期:2014-05-20  浏览次数:20673 次

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