Question

I use apache-commons-beanutils DynaBean class in order to fetch rows from a database and to handle them outside of the mysql function.

is there a way to convert a DynaBean to a List without iterating through each row and manually creating the list ?

thanks!

Was it helpful?

Solution

so far I didn't get any answers so I wrote the function that iterates through the rows and creates an ArrayList of HashMap type (String, Object).

public ArrayList<HashMap<String,Object>> convertDynaBeanListToArrayList(List<DynaBean> theList) {
    ArrayList<HashMap<String,Object>> result = new ArrayList<HashMap<String,Object>>();
    DynaProperty[] dynaProperties = null;
    for (Integer i=0;i<theList.size();i++) {
        DynaBean row = theList.get(i);
        HashMap<String,Object> resultRow=new HashMap<String,Object>();
        // each raw got the same column names, no need to fetch this for every line
        if (dynaProperties == null) {
            dynaProperties = row.getDynaClass().getDynaProperties();
        }
        for (Integer j=0;j<dynaProperties.length;j++) {
            String columnName=dynaProperties[j].getName();
            resultRow.put(columnName, row.get(columnName));
        }
        result.add(resultRow);
    }

    return result;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top