I want to create a constructor dynamically.
I have a bean class with 30 fields.
In my application I want to use this bean objects to hold the data returned from a ResultSet.
But the problem is I have dynamic columns returned by the ResultSet. some times it gives 10 & some times it will be 20.
I want to create a constructor on the fly with these dynamic columns.
Is it possible with the reflection API ? if possible, please give me sample code or link.
One way I have implemented a solution for this issue is creating a 'TransferEntity' that contains the generic values (in an array usually). Then you can wrapper this class with a "friendlier" one that has Get/Set methods. This allows you to always get the values you need (because the TransferEntity is inside) if they are variable. If not variable, you can simply use the Get/Set (which would reach inside the TransferEntity). Depending on how you're using the bean, you can simply put all the Get/Set methods inside and either return null, throw a NoSuchFieldException, etc.
Following @georgemc's suggestion I would just create a Map<String, Object> for each row. You can obtain the column names from the meta data. This would allow you to create a dataset from any random query. (I wouldn't use a Properties class unless you want to assume String values)
Something I have done in the past is used the MetaData to generate a class on the fly using ASM (caching the classes generated). However, unless you have serious efficiency issues with using a Map, I would suggest its not worth it.