我正在从 Excel 工作表中提取数据并将它们插入到我的 Oracle 表中。数据库的设置方式是,当执行批处理语句时,如果批处理中的任何插入语句失败,则批处理中的所有其他语句都不会执行。所以我的问题是如何找出实际导致问题的数据行,以便我可以向用户发送一条消息,其中包含导致问题的数据的行号?
Connection con = null;
PreparedStatement pstmt = null;
Iterator iterator = list.iterator();
int rowCount = list.size();
int currentRow = 0;
String sqlStatement = "INSERT INTO DMD_VOL_UPLOAD (ORIGIN, DESTINATION, DAY_OF_WEEK, VOLUME)";
sqlStatement += " VALUES(?, ?, ?, ?)";
int batchSize==1000;
for(currentRow=1; currentRow<=rowCount; currentRow++){
ForecastBatch forecastBatch = (ForecastBatch) iterator.next();
pstmt.setString(1, forecastBatch.getOrigin());
pstmt.setString(2, forecastBatch.getDestination());
pstmt.setInt(3, forecastBatch.getDayOfWeek());
pstmt.setInt(4, forecastBatch.getVolumeSum());
pstmt.addBatch();
if(i % batchSize == 0){
updateCounts = pstmt.executeBatch();
con.commit();
pstmt.clearBatch();
session.flush();
session.clear();
}
}
最佳答案
executeBatch
返回一个整数数组,其中包含批处理中每个语句修改的所有行的计数。我认为负数是用来表示错误的。您应该能够使用此返回值找出哪些失败。
http://docs.oracle.com/javase/1.3/docs/guide/jdbc/spec2/jdbc2.1.frame6.html
关于java - Oracle 中的批处理失败。找出批处理中的哪个语句导致它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10253226/