I have a file that contains about 1000 lines.
The functionality that I am trying to implement is to get this file, validate it, and once it is validated, I have to insert the lines into a database table 🙂
My question is: Does doing an insert in a for loop pose a major performance problem?
This for loop will call insert on each line of the file.
So we are talking about a 1000 INSERTs done via a for loop.
What would be the best way to implement something like this?
Am I doing something that is obviously wrong?
Essentially there is nothing wrong with doing 1000 inserts in a loop. For 1000 rows it’s probably not going to make a big difference, but if you really want to get the best performance you should batch your inserts using a PreparedStatement:
If you expect more rows, you might want to call executeBatch() every 1000 rows or so, because all the data (set via
addBatch()) is kept in memory until you callexecuteBatch()