根据传入的sql查询结果返回HashMap<String,String>
来源:互联网 发布:假火车票制作软件 编辑:程序博客网 时间:2024/05/20 07:32
/**
* 根据所传入的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;
}
阅读全文
0 0
- 根据传入的sql查询结果返回HashMap<String,String>
- 传入Throwable返回分好行的String
- Java String的compareTo返回结果
- 传入一个string类型的参数,然互将string的每个字符间隔一个空格返回
- ibatis 用HashMap映射sql返回的结果集
- Mybatis进行id类型、String类型、map类型、ben类型参数传入Sql查询
- Mybatis进行id类型、String类型、map类型、ben类型参数传入Sql查询
- SQLSERVER 根据传入的参数拼接sql语句字符串,反馈结果集
- Android 根据一个传入的String选择spinner里面的默认值
- HashMap<String,HashMap<String,String>> 的多级映射
- HashMap<String,String>与for合作查询和for嵌套循环查询的差别和效率
- MyBatis中,传String类型参数到xml配置文件,并执行SQL语句后返回String类型的结果写法(jeesite)
- std::string::find 空字符串 返回结果不是 string::npos
- sql in查询结果按照in的条件排序返回
- hibernate使用原生SQL查询返回结果集的处理
- 对于mybatis传入string类型的参数
- mybatis 传入String 类型的参数
- HashMap<String,short>的问题
- Oracle 查询表字段 注释 以及表的注释
- Spark Streaming之:Flume监控目录下文件内容变化,然后Spark Streaming实时监听Flume,然后从其上拉取数据,并计算出结果
- 使用git将项目上传到github
- bzoj4259 -- FFT
- Namenode
- 根据传入的sql查询结果返回HashMap<String,String>
- Git初识(完结)
- Linux命令学习总结: hexdump命令
- springboot (5) 与数据库进行交互
- Android消息机制(2)- Message和MessageQueue
- 欢迎使用CSDN-markdown编辑器
- 常用套接字选项(SOL_SOCKET级别)
- 一个例子让你了解Java反射机制
- linux中的信号2——进程如何处理信号?