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的反射方法
private Objec

本文介绍了如何通过反射将MySQL查询结果集(ResultSet)转换为自定义对象。在处理过程中,需要注意字段类型匹配,如将数据库中的int字段对应为实例类中的String类型以避免类型不匹配错误。此外,由于数据库字段名与Java Bean的setter方法相关联,若数据库字段全大写,需使用toUpperCase()进行转换匹配。
最低0.47元/天 解锁文章
725

被折叠的 条评论
为什么被折叠?



