mercredi 24 octobre 2018

How to set the values from Field(Reflection) to POJO?

I am working on dynamically mapping values from ResultSet to POJO in java. I am able to get the values but don't know how to set those value to the pojo. Any suggestions? Thanks in Advance !!

List<T> results = new ArrayList<>();
    while(resultSet.next())
    {
        T newObj = clazz.newInstance();
        for (Field field : clazz.getDeclaredFields()) 
        {
            String fieldName = field.getName().toLowerCase();
            if (columnNames.containsKey(fieldName)) 
            {
                final int index = columnNames.get(fieldName);
                field.set(fieldName, resultSet.getObject(index+1));
            }
        }
        results.add(newObj);
    }





Aucun commentaire:

Enregistrer un commentaire