问题描述
- java JDBC 批量插入的时候报数组下标越界,求大神
- 代码:
private static Boolean Function_User(Map resultMap String sqlString tableType) {
// TODO Auto-generated method stubConnection conn=null; PreparedStatement insert_st=null; int count = 0; final int batchSize = 500; System.out.println(""开始:""+Calendar.getInstance().getTime()); try { conn=DriverManager.getConnection(db_urldb_namedb_pwd); conn.setAutoCommit(false); insert_st = conn.prepareStatement(sql); for (int i = 0; i < resultMap.get(tableType).size(); i++) { Object obj = resultMap.get(tableType).get(i); OrderUserSnap user = (OrderUserSnap) obj; insert_st.setString(1 ZH_S(user.getId())); insert_st.addBatch(); if(++count % batchSize == 0) { insert_st.executeBatch(); insert_st.clearBatch(); } System.out.println(""tt:""+count); } insert_st.executeBatch(); insert_st.clearBatch(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { insert_st.close(); conn.close(); System.out.println(""结束:""+Calendar.getInstance().getTime()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return true;}
解决方案
擦 已经解决:原因是 我insert_st.setString(1 ZH_S(user.getId()));,传递的参数过多,Oracle的批处理个数限制为:
count=传参个数 * 查入的数据条数
上边设置的1000条数据,进行一次批处理,越界了~
将 final int batchSize = 500; 的值设小一点就屁事没有
解决方案二:
报错信息:
查询出的数据条数为:1450
开始:Fri Mar 04 19:32:49 CST 2016
tt:1
。
。
。
tt:999
结束:Fri Mar 04 19:32:50 CST 2016
java.lang.ArrayIndexOutOfBoundsException: -32589
at oracle.jdbc.driver.OraclePreparedStatement.setupBindBuffers(OraclePreparedStatement.java:2677)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:9255)
at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:210)
at Bing.Main.Function_User(Main.java:422)
at Bing.Main.DataForDB(Main.java:156)
at Bing.Main.ForServlet(Main.java:110)
at Bing.Main.access$0(Main.java:63)
at Bing.Main$My_Timer.run(Main.java:58)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
解决方案三:
当第1000条数据的时候,执行
if(++count % batchSize == 0) {
insert_st.executeBatch();
insert_st.clearBatch();
}
及1000条数据 提交一次,报错,说什么下标越界,找不到原因啊