The code should be database independent so I am using normal sql.
My query can change at runtime say table1 changed to table2. But lets assume for table1 query I have to update 2000 records with slot of 50, means as soon as 50 records are processed I will commit.
I have two approaches for setting values in IN clause of sql statement.
I want to know which one of the below code is more efficient (priority is optimization and maintainability is secondary
)?
PreparedStatement preparedStatement = sqlObj.prepareStatement(
"UPDATE table1 column1=? WHERE table_id IN (" + StringUtils.repeat("?,", paramArray.length-1)+ "?)");
preparedStatement.setInt(1, 0);
for(int idx = 0; idx < paramArray.length; idx++) {
preparedStatement.setInt(idx+2, paramArray[idx]);
}
preparedStatement.executeQuery();
Or
PreparedStatement preparedStatement = sqlObj.prepareStatement(
"UPDATE table1 column1=? WHERE table_id IN (?)");
for(int idx = 0; idx < paramArray.length; idx++) {
preparedStatement.setInt(1, 0);
preparedStatement.setInt(2, paramArray[idx]);
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
Edit:
Lets say param.length is 50
and this whole code exectutes 40 times
i.e. processing 2000 records
.
So in first case it will append 50 ? and then set variable for them making one update query and in second case it will create batch of 50 update queries.