1.读取数据库,T_Einteractive是我创建的实例类,如果有id字段,数据库的类型可以设置成int或者bigint,但是实例类中应该写成String类型,以免在下面利用反射时报数据类型不匹配的错,数据库表名也是T_Einteractive
public List<T_Einteractive_Question> getEinteractiveQuestions(Connection conn){
ResultSet rs = null;
List<T_Einteractive> result = new ArrayList<T_Einteractive>();
try {
String sql = "select * from T_Einteractive";
rs = conn.createStatement().executeQuery(sql);
while (rs.next()) {
T_Einteractive first = (T_Einteractive) toObject(T_Einteractive.class, rs);
if (first != null) {
result.add(first);
}
}
}catch (Exception ex){
logger.error(ex.getMessage(), ex);
return null;
}finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
}
}
return result;
}
}
2.上面的方法中用的toObject的反射方法