标签:
public static List toList(ResultSet rs, Class cls) {
List list = new ArrayList();
try {
// 获取数据库表结构
ResultSetMetaData meta = rs.getMetaData();
Object obj = null;
while (rs.next()) {
// 获取formbean实例对象
obj = Class.forName(cls.getName()).newInstance();
Field[] fields = obj.getClass().getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
int ci = rs.findColumn(fields[j].getName());
if (ci == -1)
continue;
String methodName = "set" + fields[j].getName();
Method method = obj.getClass().getMethod(methodName,
fields[j].getType());
method.invoke(obj, rs.getObject(ci));
}
list.add(obj);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
return list;
}
}
标签:
原文地址:http://www.cnblogs.com/dashi/p/4260392.html