kristenlee1218 2017-03-15 06:13 采纳率: 61.3%
浏览 2073

遍历list类型应该返回什么

方法1:public static List query(String sql, Class clazz) {

    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Object tempStr = null;
    List<T> list = new ArrayList<T>();
    try {
        conn = DBConnectUtils.getConnection();
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            // newInstance实际上是把new这个方式分解为两步,即,首先调用class的加载方法加载某个类,然后实例化。
            // 我们可以在调用class的静态加载方法forName时获得更好的灵活性,提供给了我们降耦的手段。
            // newInstance: 弱类型。低效率。只能调用无参构造。
            // new: 强类型。相对高效。能调用任何public构造。
            T obj = clazz.newInstance();
            if (obj instanceof String) {
                obj = (T) rs.getString(1);
            } else {

                // Field 提供有关类或接口的单个字段的信息,以及对它的动态访问权限

                Field[] fields = obj.getClass().getDeclaredFields();

                for (Field field : fields) {

                    try {
                        tempStr = rs.getObject(field.getName());
                    } catch (SQLException e) {
                        tempStr = "";
                    }
                    //置是否允许访问,而不是修改原来的访问权限修饰词
                    field.setAccessible(true);

                    // 把对象的属性数据封装到对象中
                    BeanUtils.setProperty(obj, field.getName(), tempStr);
                }
            }
            list.add(obj);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        DBCloseUtils.closeCSR(conn, pstmt, rs);
    }
    return list;

}

方法2:
public static List query(String sql, Class clazz, Object[] parameters) {

    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    List<T> list = new ArrayList<T>();
    int index = 1;
    try {
        pstmt = conn.prepareStatement(sql);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    if (parameters != null && parameters.length != 0) {
        for (int i = 0; i < parameters.length; i++) {
            try {
                pstmt.setObject(index, parameters[i]);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    try {
        rs = pstmt.executeQuery(sql);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    // 封装resultset
    ResultSetMetaData metaData = null;
    try {
        metaData = rs.getMetaData();
    } catch (SQLException e) {
        e.printStackTrace();
    }// 取出列的信息
    int columnLength = 0;
    try {
        columnLength = metaData.getColumnCount();
    } catch (SQLException e) {
        e.printStackTrace();
    }// 获取列数
    try {
        while (rs.next()) {
            // 通过反射机制创建一个对象
            T result = clazz.newInstance();
            for (int i = 0; i < columnLength; i++) {
                String metaDataKey = metaData.getColumnName(i + 1);
                Object resultsetValue = rs.getObject(metaDataKey);
                if (resultsetValue == null) {
                    resultsetValue = "";
                }
                Field field = clazz.getDeclaredField(metaDataKey);
                field.setAccessible(true);
                field.set(result, resultsetValue);
            }
            list.add(result);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return list;
}
  • 写回答

1条回答

  • zhangle_123 2017-03-15 06:40
    关注

    waht are you 弄啥来?你想表达个什么意思! try {
    conn = DBConnectUtils.getConnection();
    pstmt = conn.prepareStatement(sql);
    rs = pstmt.executeQuery();
    while (rs.next()) {
    你这不是有么,你把查询的sql初始化一下不就好了

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配