【问题标题】:preparedstatement with multiple rows where some row values are not set具有多行的preparedstatement,其中某些行值未设置
【发布时间】:2014-04-03 01:28:45
【问题描述】:

我有一个这样的准备好的声明

insert into mytable (id, name) values (?,?) , (?,?);

我在每个preparedStatement 中使用多行,因为我看到了巨大的速度提升。

现在,如果我要输入奇数行,那么 preparedStatement.executeBatch() 不会在数据库中输入任何行。它不会抛出任何错误。

这是我插入值的方式

int count =0;
for(int i=0; i<size; i++) {
    statement.setObject(1, id[i]);
    statement.setObject(2, name[i]);

    //second row
    if(i+1 != size) {
        statement.setObject(1, id[i+1]);
        statement.setObject(2, name[i+1]);
    }

    statement.addBatch();
    if (count % 200 == 0 && count >0) {
        statement.executeBatch();
    }
}
statement.executeBatch();

我该怎么做才能让它发挥作用?

【问题讨论】:

  • 您可以有多个 PreparedStatement,每个用于一组不同的输入
  • @codeNinja:大小是如何计算的?您能否添加更多在您发布的代码之前执行的代码。

标签: java mysql prepared-statement


【解决方案1】:

您可以使用 MySQL 驱动程序中的“rewriteBatchedStatements”选项自动执行此操作。您可以编写单个插入语句并将其作为批处理执行,驱动程序将自动为您重写它,以便在尽可能少的往返行程中执行。参考文献http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html

使用此解决方案,您不必使用 INSERT 的多行形式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多