rs2list 相关代码
public static List rs2list(ResultSet rs) throws Exception {
List list = new ArrayList();
if (rs == null) {
return list;
}
String[] arr = null;
Map map = null;
arr = getColName(rs);
int i = 0;
int num = 0;
String v = null;
if (arr != null) {
num = arr.length;
while (rs.next()) {
map = new HashMap();
for (i = 1; i <= num; i++) {
v = rs.getString(i);
if (v == null) {
v = "";
}
map.put(arr[i - 1], v);
}// end for
list.add(map);
}// end while
}// end if
return list;
}
//支持分页,基于结果集的通用"假"分页
//把结果集全部装载到内存,(数据量不大时完全可以这么做)
//对于大数据量查询,请设置statementset的setMaxRows参数,限制resultSet大小即可
//要不然会很慢且吃内存
public static List rs2list(ResultSet rs, int skip, int page_size)
throws Exception {
List list = new ArrayList();
if (rs == null) {
return list;
}
String[] arrColName = null;
String colName = null;
int colNum = 0;
int pos = 0;
int i = 0;
int j = 0;
Map map = null;
String v = null;
ResultSetMetaData rsmd = null;
rsmd = rs.getMetaData();
colNum = rsmd.getColumnCount();
arrColName = new String[colNum];
for (i = 1; i <= colNum; i++) {
arrColName[i - 1] = rsmd.getColumnName(i).toLowerCase();
}
if (page_size == 0) {
page_size = 10000000;
}
while ((rs.next()) && (j < page_size)) {
if (pos < skip) {
pos++;
continue;
}
map = new HashMap();
for (i = 0; i < colNum; i++) {
v = rs.getString(i + 1);
if (v == null) {
v = "";
}
map.put(arrColName[i], v);
}
j++;
list.add(map);
}// end while
return list;
}
public static String[] getColName(ResultSet rs) throws Exception {
if (rs == null) {
return null;
}
int num = 0;
int i = 0;
String colName = null;
String[] arr = null;
ResultSetMetaData rsmd = null;
rsmd = rs.getMetaData();
num = rsmd.getColumnCount();
arr = new String[num];
for (i = 1; i <= num; i++) {
colName = rsmd.getColumnName(i);
colName = colName.toLowerCase();
arr[i - 1] = colName;
}
return arr;
}
以前也写的sqlmap可参考这里
http://www.javaresearch.org/article/50600.htm
enjoy!
giscat,2006.12.21
