我在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();
}
});
}
答案 0 :(得分:0)
将条目定义为最终类似:
for (final Map.Entry<String, String> entry : myMap.entrySet())