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 管先飞 阅读(489) 评论(0)  编辑  收藏 所属分类: Jfinal

只有注册用户登录后才能发表评论。


网站导航: