mysql batch insert : get the number of inserted lines
-
05-06-2021 - |
Question
I'm using java to do batch inserts into a mysql table:
cnx = lconnect.openConnection();
mStatement = cnx.createStatement();
boolean firstTime = true;
PreparedStatement preparedStatement = null;
preparedStatement = cnx.prepareStatement(strQuery);
preparedStatement.setString(param1);
preparedStatement.setString(param2);
....
preparedStatement.addBatch();
preparedStatement.setString(param1);
preparedStatement.setString(param2);
preparedStatement.addBatch();
preparedStatement.setString(param1);
preparedStatement.setString(param2);
preparedStatement.execute();
Is there any way to really know the number of inserted lines ?
I think my piece of code it's working (?) by the number of lines in the DB does not match with the number of lines supposed to be inserted, so I'm thinking I have a problem with y inserts maybe.
Solution
You have to call executeBatch()
. The returned array contains the information you want.
From the JavaDoc:
Returns: an array of update counts containing one element for each command in the batch. The elements of the array are ordered according to the order in which commands were added to the batch.
OTHER TIPS
Use PreparedStatement
object for each batch record :)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow