java - Using reflection to set all of an object’s fields from SQL results -
the java tutorials oracle include page, class literals runtime-type tokens.
the code there shows how passing class
object argument can used generics define type of object being instantiated newinstance()
. example code goes on mention in comments:
use reflection , set of item's fields sql results.
collection<empinfo> emps = sqlutility.select(empinfo.class, "select * emps"); ... public static <t> collection<t> select(class<t> c, string sqlstatement) { collection<t> result = new arraylist<t>(); /* run sql query using jdbc. */ (/* iterate on jdbc results. */ ) { t item = c.newinstance(); /* use reflection , set of item's * fields sql results. */ result.add(item); } return result; }
i wondering such code looks use reflection populate object’s fields results of row resultset
.
➠does care post example of such code?
my interest see example of reflection in action, , see how 1 might match members/properties of object fields of database row. whether approach optimal or recommended not issue, need references jdbc frameworks needed here.
Comments
Post a Comment