guanxf

          我的博客:http://blog.sina.com.cn/17learning

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            71 隨筆 :: 1 文章 :: 41 評論 :: 0 Trackbacks
          問題原因:JFinal中根據索引查詢列名。
          1、重新新建DB類:
          package com.microcorecn.jfinalex;
          import java.sql.Connection;
          import java.sql.PreparedStatement;
          import java.sql.ResultSet;
          import java.sql.SQLException;
          import java.util.ArrayList;
          import java.util.List;
          import com.jfinal.plugin.activerecord.ActiveRecordException;
          import com.jfinal.plugin.activerecord.DbKit;
          public class DbEx {
          static <T> List<T> query(Connection conn, String sql, Object... paras) throws SQLException {
          List result = new ArrayList();
          PreparedStatement pst = conn.prepareStatement(sql);
          for (int i=0; i<paras.length; i++) {
          pst.setObject(i + 1, paras[i]);
          }
          // for (int i=0; i<paras.length; i++) {
          // pst.setObject(i + 1, paras[i]);
          // }
          ResultSet rs = pst.executeQuery();
          int colAmount = rs.getMetaData().getColumnCount();
          String[] cols = new String[colAmount];
          for(int i = 0; i < colAmount; ++i)
          {
          cols[i] = rs.getMetaData().getColumnLabel(i + 1);
          }
          if (colAmount > 1) {
          while (rs.next()) {
          Object[] temp = new Object[colAmount];
          for (int i=0; i<colAmount; i++) {
          temp[i] = rs.getObject(cols[i]);
          }
          result.add(temp);
          }
          }
          else if(colAmount == 1) {
          while (rs.next()) {
          result.add(rs.getObject(cols[0]));
          }
          }
          if (rs != null) {try {rs.close();} catch (SQLException e) {}}
          if (pst != null) {try {pst.close();} catch (SQLException e) {}}
          return result;
          }
          public static <T> List<T> query(String sql, Object... paras) {
          Connection conn = null;
          try {
          conn = DbKit.getConnection();
          return query(conn, sql, paras);
          } catch (Exception e) {
          throw new ActiveRecordException(e);
          } finally {
          DbKit.close(conn);
          }
          }
          }
          2、查詢信息:
          List<Object[]>  newAlbum=DbEx.query(sb.toString(), singerItem.get("id"));
          if (newAlbum!= null) {
          for(Object[] obj:newAlbum){
          item.setBfpSingerId(obj[0]==null?"":obj[1].toString());
          item.setSingerName(obj[1]==null?"":obj[1].toString());
          item.setImgUrl(obj[2]==null?"":obj[2].toString());
          item.setBfpAlbumId(obj[3]==null?"":obj[3].toString());
          item.setNewAlbumName(obj[4]==null?"":obj[4].toString());
          }

          posted on 2013-04-02 18:40 管先飛 閱讀(504) 評論(0)  編輯  收藏 所屬分類: Jfinal
          主站蜘蛛池模板: 璧山县| 宜城市| 绥化市| 开封县| 洛宁县| 收藏| 年辖:市辖区| 锡林郭勒盟| 安溪县| 紫阳县| 阜新市| 云阳县| 忻城县| 乌鲁木齐市| 珠海市| 会同县| 墨玉县| 巴楚县| 和平县| 韩城市| 绥宁县| 肥城市| 黄山市| 图木舒克市| 阿瓦提县| 砀山县| 尖扎县| 岑巩县| 禹城市| 苗栗县| 惠来县| 高台县| 虞城县| 新巴尔虎右旗| 山阳县| 舒兰市| 南靖县| 株洲县| 定日县| 塔城市| 阜康市|