在项目中用到了JDBC的批处理Batch,程序偶尔会报错,e.getMessage()只得到了一个单词“Error”
有时可以正常执行通过,有时会抛SQLException的异常,感觉很奇怪,异常信息也很奇怪代码大致如下:
private void BatchInsertData(List<BeanClass> list, Connection connection) {
try{
PrepareStatement ps = connection.prepareStatement("insert into A(A_ID, A_NAME, A_ENGLISH_NAME, A_ADDERSS, A_PHONE_NUMBER)VALUES(A_SEQUENCE.NEXTVAL, ?, ?, ?, ?)");
list.forEach(beanClass -> {
if(beanClss.getName()==null) {
ps.setNull(1, Types.VARCHAR);
} else {
ps.setString(1, beanClss.getName())
}
if(beanClss.getEnglishName()==null) {
ps.setNull(2, Types.VARCHAR);
} else {
ps.setString(2, beanClss.getEnglishName())
}
...
...
ps.addBatch(); //加入批处理
})
ps.executeBatch();//执行批处理
conn.commit();
} catch (SQLException e) {
conn.rollback();
System.err.println(String.format("SQLException: Message[%s];SQLState[%s];ErrorCode[%s]", e.getMessage(), e.getSQLState(), e.getErrorCode()));
} finally {
if(ps != null){
ps.close();
}
}
}
}
输出的异常信息:
SQLException: Message[Error];SQLState[null];ErrorCode[0]
求大神答疑