JDBC中的executebatch()只插入少量记录

时间:2018-01-04 20:06:58

标签: java mysql jdbc

我在JDBC中遇到批处理的问题,我在批处理中添加了一个准备好的语句,但是当我执行批处理时,只插入了几条记录,我搜索了一下,我没想到它,这里是码: 我是另一个准备好的声明的结果集,这是一个问题吗?谢谢!

 //autocommitFALSE (...)
 while(rs.next()){ 



            int idrobot = rs.getInt("idRobot");  
            if (idrobot != idrn) {
                  String fullstatenew2 = "INSERT INTO `fullstate`(`idR`, `idC`, `s1`, `s2`, `s3`, `s4`, `s5`, `s6`, `s7`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
                  java.sql.PreparedStatement preparedStmt9 = conn.prepareStatement(fullstatenew2);

                    preparedStmt9.setInt(1, robot);
                    preparedStmt9.setString(2, cluster);
                    preparedStmt9.setInt(3, s1);
                    preparedStmt9.setInt(4, s2);
                    preparedStmt9.setInt(5, s3);
                    preparedStmt9.setInt(6, s4);
                    preparedStmt9.setInt(7, s5);
                    preparedStmt9.setInt(8, s6);
                    preparedStmt9.setInt(9, s7);

                    preparedStmt9.addBatch();

                    if(i>10000) //it's really not like this, but this is the sense {
                        preparedStmt9.executeBatch();
                        System.out.println("insert");
                    }
                    conn.commit();

1 个答案:

答案 0 :(得分:0)

添加批处理应该在循环中,执行批处理应该在循环之后。