public static <T> List<T> getList(Class<T> cla, String sql, Object... args) throws Exception {
List<T> list = new ArrayList<T>();
Connection connection = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
connection = getConnection();
st = connection.prepareStatement(sql);
for (int i = 1; i <= args.length; i++) {
st.setObject(i, args[i - 1]);
}
rs = st.executeQuery();
List<String> colLabels = new ArrayList<>();
ResultSetMetaData rsmd = rs.getMetaData();
int col = rsmd.getColumnCount();
for (int i = 0; i < col; i++) {
colLabels.add(rsmd.getColumnName(i + 1));
}
Map<String, Object> map = null;
while (rs.next()) {
map = new HashMap<String, Object>();
for (String colName : colLabels) {
map.put(colName, rs.getObject(colName));
}
T object = cla.newInstance();
for (Map.Entry<String, Object> entry : map.entrySet()) {
String propertyName = entry.getKey();
Object value = entry.getValue();
setProperty(object, propertyName,value);
}
list.add(object);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCTools.release(connection, st, rs);
}
return list;
}