У меня есть карта в java.
Я хочу вставить его в таблицу базы данных, используя JdbcTemplate.
Я написал ниже код для того же:
for (Map.Entry<String, String> entry : myMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
this.stgdbJdbcTemplate.batchUpdate(this.myInsertQuery, new BatchPreparedStatementSetter() {
@Override
public void setValues(final PreparedStatement ps, final int i) throws SQLException {
ps.setString(1, entry.getKey());
ps.setString(2, entry.getValue());
}
});
}
Здесь myMap - это локальная переменная, как показано ниже:
Map<String, String> myMap = new HashMap<String, String>();
Однако, я становлюсь ниже ошибки:
Cannot refer to a non-final variable entry inside an inner class defined in a different method
Что может быть решением этого?
Обновленный код:
for (final Map.Entry<String, String> entry : myMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
this.stgdbJdbcTemplate.batchUpdate(this.myInsertQuery, new BatchPreparedStatementSetter() {
@Override
public void setValues(final PreparedStatement ps, final int i) throws SQLException {
ps.setString(1, entry.getKey());
ps.setString(2, entry.getValue());
}
public int getBatchSize() {
return finalFields.size();
}
});
}
Определите запись как final:
for (final Map.Entry<String, String> entry : myMap.entrySet())