How to access elements of a BasicDynaBean type of object in Java?

405 Views Asked by At

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?

1

There are 1 best solutions below

0
andrewJames On

The following code populates a list of DynaBeans using a JdbcTemplate. It then accesses the resulting list and makes changes to the beans' data:

// create the fields we need for our students bean:
DynaProperty[] beanProperties = new DynaProperty[]{
    new DynaProperty("name", String.class),
    new DynaProperty("marks", Integer.class)
};

// create a class containing these fields:
BasicDynaClass studentClass = new BasicDynaClass("student",
        BasicDynaBean.class, beanProperties);

String query = "SELECT * FROM students";

// handle the result set using a jdbcTemplate:
List<DynaBean> students = jdbcTemplate.query(query, null, null, (ResultSet rs) -> {
    // define a list which will hold the DB results in a list of DynaBeans:
    List<DynaBean> dynaBeanList = new ArrayList<>();
    while (rs.next()) {
        // the DynaBean which will hold the current table record:
        DynaBean student = null;
        try {
            student = studentClass.newInstance();
        } catch (IllegalAccessException | InstantiationException ex) {
            log.error("DynaBean creation failed.", ex);
        }
        // load the current table record into the DynaBean:
        if (student != null) {
            student.set("name", rs.getString("name"));
            student.set("marks", rs.getInt("marks"));
            dynaBeanList.add(student);
        }
    }
    return dynaBeanList;
});

// demonstrate that we can process our list of DynaBeans:
if (students != null) {
    students.forEach(student -> {
        // Add 1 to each student's marks:
        student.set("marks", (Integer) student.get("marks") + 1);
        // print the student's name and the new marks:
        System.out.println(student.get("name") + ": " + student.get("marks"));
    });
}

The table was loaded in advance with the following data:

jdbcTemplate.update("INSERT INTO students(name, marks) VALUES (?,?)", "John Jones", 78);
jdbcTemplate.update("INSERT INTO students(name, marks) VALUES (?,?)", "Mary Smith", 89);

The following output was generated, showing we can access the resulting dynabeans and update their data:

John Jones: 79
Mary Smith: 90

In my example, I used the following JdbcTemplate method using a result set extractor to wrap the JDBC result set (see here):

jdbcTemplate.query(query, null, null, (ResultSet rs) -> { ... });

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.