当前位置: 代码迷 >> J2SE >> 数据库查询出记录封装到对象中,该怎么解决
  详细解决方案

数据库查询出记录封装到对象中,该怎么解决

热度:43   发布时间:2016-04-24 12:18:22.0
数据库查询出记录封装到对象中
从一个数据库中查出5条数据,我想把这五条数据的每一条作为一个结果集封装到emp对象中,然后把对象封装到list中,代码怎么写,急用,在线等

------解决方案--------------------
List list = new List();
Emp e = new Emp();
e.setId(1);
e.setName(a);
e.setAge(11);
list.add(e);

当然set进去的值需要你自已从数据库里拉出来。
------解决方案--------------------
while(rs.next()){
list.add(new Emp(rs.getInt("id"),rs.getString("name"),rs.getInt("age"));
}
------解决方案--------------------
给你我写过的一个类参考。利用了java泛型和反射,使用前须创建好与该数据库表相应的pojo类,该javabean中的每个字段和你数据库中表的字段名字相同。直接继承该类即可,方法返回一个封装了该pojo的list集合

Java code
public class Query<T> {    private DBUtil db = DBUtil.getInstance();    private List<T> list = null;    private int totleCount = -1;    private Class<T> entityClass;    private String sql = null;    @SuppressWarnings("unchecked")    public Query(String sql) {        this.entityClass = (Class<T>) ((ParameterizedType) getClass()                .getGenericSuperclass()).getActualTypeArguments()[0];        this.sql = sql;        this.list = excute(sql);    }            public int getCount() {        return totleCount;    }    public List<T> getGridData(int start, int size) {        if (start + size > totleCount) {            size = totleCount - start;        }        return list.subList(start, start + size);    }    public List<T> getQueryData() {        return list;    }    public List<T> excute(String sql) {        ArrayList<T> list = null;        Statement stm = db.getStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,                ResultSet.CONCUR_READ_ONLY);        ResultSet rst = null;        try {            rst = stm.executeQuery(sql);            rst.last();            totleCount = rst.getRow();            rst.beforeFirst();            list = new ArrayList<T>(totleCount);            while (rst.next()) {                ResultSetMetaData metaData = rst.getMetaData();                T t = entityClass.newInstance();                for (int j = 1; j <= metaData.getColumnCount(); j++) {                    String name = metaData.getColumnName(j);                    try {                        Field field = entityClass.getDeclaredField(name                                .substring(0, 1).toLowerCase()                                + name.substring(1));                        Method method = entityClass.getMethod("set"                                + name.substring(0, 1).toUpperCase()                                + name.substring(1), field.getType());                        String type = field.toGenericString();                        if (type.contains("java.lang.String")) {                            method.invoke(t, rst.getString(name));                        } else if (type.contains("java.lang.Integer")) {                            method.invoke(t, rst.getInt(name));                        } else if (type.contains("java.lang.Double")) {                            method.invoke(t, rst.getDouble(name));                        } else if (type.contains("java.sql.Date")) {                            method.invoke(t, rst.getDate(name));                        } else if (type.contains("java.sql.Timestamp")) {                            method.invoke(t, rst.getTimestamp(name));                        }                    } catch (NoSuchFieldException e) {                        /*                         * System.out.println("找不到字段" + name.substring(0,                         * 1).toLowerCase() + name.substring(1) + ",将忽略处理。");                         */                    }                }                list.add(t);            }        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            db.freeResource(stm, rst);        }        return list;    }    }
  相关解决方案