Note about how to do batch operation in database using JDBC.
Because UPDATE, DELETE, SELECT it self can deal with multiple data, so they don’t need to batch. Only INSERT need batch operation.
Statement
1 2 3 4 5 6
Connectionconn= JDBCUtils.getConnection(); Statementst= conn.createStatement(); for(inti=0; i < 20000; i ++) { Stringsql="insert into goods(name)values('name_" + i + "')"; st.execute(sql); }
This method is still slow because it still need to IO in disk 20000 times.
Using Batch
using addBatch(), executeBatch(), clearBatch()
MySQL does not support batch by default, we need to add a parameter at the end of URL when getting the connection to database. ?rewriteBatchedStatements=true