批量入库时报错:prepared statement contains too many placeholders

来源:互联网 发布:瑞易物管软件 编辑:程序博客网 时间:2024/05/15 16:04

Mysql的批量入库时,执行到PreparedStatment.executeBatch()这一句时,报错:prepared statement contains too many placeholders,提示占位符过多


原因:

在批量入库时,Mysql会将insert语句拼装成如下格式:

insert into tablename(字段1, 字段2, 字段3, ……, 字段45) values(:字段1, :字段2, :字段3, ……,  :字段45), (:字段1, :字段2, :字段3, ……,  :字段45), (:字段1, :字段2, :字段3, ……,  :字段45), ……,  (:字段1, :字段2, :字段3, ……,  :字段45)

比如我批量入库的对象是3000+条记录,虽然我把它封装成一个List集合,但是集合里面每个元素都有45个key(一个key对应一个字段)。sql被拼装成上述格式后,占位符的个数达到3000*45=135000,大于mysql对占位符的最高限制65535。

所以需要注意的是,批量入库时,占位符的个数不再是45,而是45与条数的乘积。


0 0
原创粉丝点击