hjw1197052444
2016-03-04 11:34
采纳率: 75%
浏览 2.2k
已采纳

java JDBC 批量插入的时候报数组下标越界,求大神

代码:
private static Boolean Function_User(Map resultMap, String sql,String tableType) {
// TODO Auto-generated method stub

    Connection conn=null;
    PreparedStatement insert_st=null;
    int count = 0;
    final int batchSize = 500;
    System.out.println("开始:"+Calendar.getInstance().getTime());
    try {
        conn=DriverManager.getConnection(db_url,db_name,db_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;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • hjw1197052444 2016-03-04 11:42
    已采纳

    擦 已经解决:原因是 我insert_st.setString(1, ZH_S(user.getId()));,传递的参数过多,Oracle的批处理个数限制为:
    count=传参个数 * 查入的数据条数

    上边设置的1000条数据,进行一次批处理,越界了~
    将 final int batchSize = 500; 的值设小一点就屁事没有

    点赞 打赏 评论
  • hjw1197052444 2016-03-04 11:36

    报错信息:
    查询出的数据条数为: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)

    点赞 打赏 评论
  • hjw1197052444 2016-03-04 11:37

    当第1000条数据的时候,执行
    if(++count % batchSize == 0) {
    insert_st.executeBatch();
    insert_st.clearBatch();
    }
    及1000条数据 提交一次,报错,说什么下标越界,找不到原因啊

    点赞 打赏 评论

相关推荐 更多相似问题