Java: Insert multiple rows into MySQL with PreparedStatement

后端 未结 6 1258
后悔当初
后悔当初 2020-11-22 16:28

I want to insert multiple rows into a MySQL table at once using Java. The number of rows is dynamic. In the past I was doing...

for (String element : array)          


        
6条回答
  •  执念已碎
    2020-11-22 16:58

    When MySQL driver is used you have to set connection param rewriteBatchedStatements to true ( jdbc:mysql://localhost:3306/TestDB?**rewriteBatchedStatements=true**).

    With this param the statement is rewritten to bulk insert when table is locked only once and indexes are updated only once. So it is much faster.

    Without this param only advantage is cleaner source code.

提交回复
热议问题