我正在尝试在Oracle表中插入记录,并使用java8和ojdbc6作为技术。我正在使用批处理将记录插入表中。我故意提供错误的记录以获取BatchUpdateException。当控件流向其中捕获块时,我试图获取错误之前的更新记录数,并将0作为计数。但是在表中,我可以看到成功插入了4条记录。请帮我。
请参见下面的代码。
public class TestPrepare {
public static void main(String[] args) throws SQLException{
int arr[] = null;
PreparedStatement st = null;
try {
Connection connection=getConnection();
st =connection.prepareStatement("Insert into test_dummy(first,second) values (?,?)");
st.setString(1, "Akshaya23"); st.setInt(2, 7);
st.addBatch();
st.setString(1, "Akshaya24"); st.setInt(2, 8);
st.addBatch();
st.setString(1, "Akshaya26"); st.setInt(2, 4);
st.addBatch();
st.setString(1, "Akshaya22"); st.setInt(2, 5);
st.addBatch();
st.setString(1, "Akshaya25"); st.setString(2, "Kaushik");
st.addBatch();
arr=st.executeBatch();
System.out.println("Dekh le isko bhi "+arr.length);
System.out.println("Command executed Successfully");
}
catch(BatchUpdateException ex) {
ex.printStackTrace();
arr=ex.getUpdateCounts();
System.out.println("the number of update counts is "+ex.getUpdateCounts().length);
System.out.println("Count of no info success "+st.SUCCESS_NO_INFO);
System.out.println("Count of failed records "+st.EXECUTE_FAILED);
}
}
下面是表快照(Column1(类型)-varchar,Column2(类型)-数字)
错误和sysout语句
java.sql.BatchUpdateException:ORA-01722:无效的数字
更新计数为0 无信息成功计数-2 失败记录数-3 在oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10407) 在oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:230) 在练习时.TestPrepare.main(TestPrepare.java:31)