I am working on a Spring Boot app where I am using JdbcTemplate to return a List<BasicDynaBean> from a SQL query like this:
List<BasicDynaBean> students= SELECT * from students; //where each students row has following data fields: `name`,`marks`.
for(BasicDynaBean student:students)
//increment marks
Now, I want to iterate through the students list and do some modifications on marks. How can I do this?
The following code populates a list of
DynaBeansusing aJdbcTemplate. It then accesses the resulting list and makes changes to the beans' data:The table was loaded in advance with the following data:
The following output was generated, showing we can access the resulting dynabeans and update their data:
In my example, I used the following
JdbcTemplatemethod using a result set extractor to wrap the JDBC result set (see here):I would typically use a prepared statement instead of a hard-coded SQL statement in a string - and therefore I would use a different method, accordingly.
The above sample used Spring Boot v2.5.4 with Commons Beanutils v1.9.4. It also used Java 15, but should work the same way with Java 8.