问题原因: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());
}
}