//sql: 查询语句;
//valueList: 参数值列表
public static List select(String sql, List valueList) throws SQLException {
ArrayList returnList = new ArrayList();
PreparedStatement ps = GetConnection.getConnection().prepareStatement(
sql);
if (valueList != null && valueList.size() != 0) {
for (int i = 0; i < valueList.size(); i++) {
ps.setObject(i + 1, valueList.get(i));
}
}
ResultSet rs = ps.executeQuery();
int colCount = rs.getMetaData().getColumnCount();
while (rs.next()) {
HashMap rowMap = new HashMap();
for (int i = 0; i < colCount; i++) {
String colName = rs.getMetaData().getColumnName(i + 1)
.toLowerCase();
String colValue = rs.getString(i + 1);
rowMap.put(colName, colValue);
}
returnList.add(rowMap);
}
rs.close();
ps.close();
return returnList;
}
将结果集的列作为KEY,值作为VALUE放入Map中,用list装起来。(适用于普遍查询,需知道列名,较麻烦)