后台是MySql,大家有没有碰到添加了很多addBatch,但是最终commit完,只
执行最后一条的情况。
conn = BaseDaoLocalImpl.getBase().getConn();
conn.setAutoCommit(false);
sql = "insert into course_charge_standard (big_num,course_name,course_level,course_type,course_std,charge_type," +//6列
"start_date,course_object,course_info,course_memo,branch_num) values(?,?,?,?,?,?,?,?,?,?,?)";//5列
for(int i =0; i<ccsList.size();i++){
Course_charge_standard ccs = ccsList.get(i);
pstmt = conn.prepareStatement(sql);
pstmt.setLong(1, ccs.getBig_num());
pstmt.setString(2, ccs.getCourse_name());
pstmt.setString(3, ccs.getCourse_level());
pstmt.setString(4,ccs.getCourse_type());
pstmt.setBigDecimal(5, ccs.getCourse_std());
pstmt.setString(6, ccs.getCharge_type());
pstmt.setString(7, ccs.getStart_date());
pstmt.setString(8, ccs.getCourse_object());
pstmt.setString(9, ccs.getCourse_info());
pstmt.setString(10, ccs.getCourse_memo());
pstmt.setInt(11, ccs.getBranch_num());
pstmt.addBatch();
}
int[] executeBatch = pstmt.executeBatch();//这里只返回了1
for (int j : executeBatch) {
System.out.println(" j="+j);
}
conn.commit();
但是如果在
pstmt.addBatch();后面紧跟一句 pstmt.executeBatch();,即每addBatch一次,就executeBatch一次,就没有问题。
------解决方案--------------------
把pstmt = conn.prepareStatement(sql);移到for 循环外面。
------解决方案--------------------
pstmt = conn.prepareStatement(sql); 这个 问题了, 你放循环外面。