/**
* 根据所传入的sql查询,以Hashtable数组返回结果集.
* 取值形式 :Hashtable[i]..get(字段名),字段名须小写。
* @param sql sql查询语句
* @return Hashtable[] Hashtable数组
*/
public HashMap<String,String>[] getQuery(String sql) throws SeException {
HashMap<String,String>[] hashRet = null;
hashRet = (HashMap[])getCache(sql);//获取缓存,根据sql里面的表名和主键:主键值 通过request.getAttribute("Cache[TABLE_NAEM:primaryKey:主键值]");来获取
if(hashRet==null){
Vector vetRet = new Vector();
String fieldName = null;
String fieldType = null;
String fieldValue = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
//纪录集字段数
ResultSetMetaData resultMeta = rs.getMetaData();//得到结果集的结构信息,比如字段名,字段数等
int columns = resultMeta.getColumnCount(); //返回数据集的列数
HashMap record = new HashMap();
int i = 0;
int rows = 0;
while (rs.next()) {
record.clear();
for (i = 1; i <= columns; i++) {
fieldName = new String(resultMeta.getColumnLabel(i));//得到每一列的列名
fieldName = fieldName.toLowerCase();
fieldType = new String(resultMeta.getColumnTypeName(i));//得到每一列的类型
fieldType = fieldType.toLowerCase();
// if (getDbType().equals("ORACLE") && fieldType.equals("clob")) {
// oracle.sql.CLOB clobVal = (oracle.sql.CLOB) rs.getObject(fieldName);
// if (clobVal != null) {
// fieldValue = clobVal.getSubString(1l, (new Long(clobVal.length())).intValue());
// }
// } else {
fieldValue = rs.getString(fieldName);//得到相应列的值
// }
record.put(fieldName, fieldValue);
}/// end for
vetRet.addElement(record.clone());
rows++;
}/// end while
if (rows == 0) {
return null;
}
hashRet = new HashMap[vetRet.size()];
vetRet.copyInto(hashRet);
resultMeta = null;
record = null;
vetRet = null;
fieldName = null;
fieldValue = null;
} catch (Exception e) {
throw new SeException(e.getMessage());
} finally {
log.debug(sql);//记录在日志文件
if (rs != null) {
try {
rs.close();
} catch (Exception ignore) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception ignore) {
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception ignore) {
}
}
rs = null;
stmt = null;
conn = null;
}
addCache(sql, hashRet);//记录缓存
}
return hashRet;
}
* 根据所传入的sql查询,以Hashtable数组返回结果集.
* 取值形式 :Hashtable[i]..get(字段名),字段名须小写。
* @param sql sql查询语句
* @return Hashtable[] Hashtable数组
*/
public HashMap<String,String>[] getQuery(String sql) throws SeException {
HashMap<String,String>[] hashRet = null;
hashRet = (HashMap[])getCache(sql);//获取缓存,根据sql里面的表名和主键:主键值 通过request.getAttribute("Cache[TABLE_NAEM:primaryKey:主键值]");来获取
if(hashRet==null){
Vector vetRet = new Vector();
String fieldName = null;
String fieldType = null;
String fieldValue = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
//纪录集字段数
ResultSetMetaData resultMeta = rs.getMetaData();//得到结果集的结构信息,比如字段名,字段数等
int columns = resultMeta.getColumnCount(); //返回数据集的列数
HashMap record = new HashMap();
int i = 0;
int rows = 0;
while (rs.next()) {
record.clear();
for (i = 1; i <= columns; i++) {
fieldName = new String(resultMeta.getColumnLabel(i));//得到每一列的列名
fieldName = fieldName.toLowerCase();
fieldType = new String(resultMeta.getColumnTypeName(i));//得到每一列的类型
fieldType = fieldType.toLowerCase();
// if (getDbType().equals("ORACLE") && fieldType.equals("clob")) {
// oracle.sql.CLOB clobVal = (oracle.sql.CLOB) rs.getObject(fieldName);
// if (clobVal != null) {
// fieldValue = clobVal.getSubString(1l, (new Long(clobVal.length())).intValue());
// }
// } else {
fieldValue = rs.getString(fieldName);//得到相应列的值
// }
record.put(fieldName, fieldValue);
}/// end for
vetRet.addElement(record.clone());
rows++;
}/// end while
if (rows == 0) {
return null;
}
hashRet = new HashMap[vetRet.size()];
vetRet.copyInto(hashRet);
resultMeta = null;
record = null;
vetRet = null;
fieldName = null;
fieldValue = null;
} catch (Exception e) {
throw new SeException(e.getMessage());
} finally {
log.debug(sql);//记录在日志文件
if (rs != null) {
try {
rs.close();
} catch (Exception ignore) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception ignore) {
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception ignore) {
}
}
rs = null;
stmt = null;
conn = null;
}
addCache(sql, hashRet);//记录缓存
}
return hashRet;
}