您的位置:首页 > 其它

将任意查询结果ResultSet转化成List<Map>

2013-03-21 03:52 477 查看
在JadePool中,提供了若干个查询方法,分别是:

public List query(String sqlquery, int position, int length, boolean isScrollSenstive) throws SQLException

public List query(String sqlquery, int timeout) throws SQLException

public List query(String sqlquery) throws SQLException

它们支持任意复杂的SQL查询,只要受DBMS支持,只要没有语法错误均可

在这些方法中,每次完成ResultSet到List<Map>的转换后,立即关闭了ResultSet对象,它们均调用底层的核心方法

private void _recordMappingToMap(String fieldClassName, String fieldName, ResultSet rs, Map fieldValue) throws SQLException

在_recordMappingToMap中的参数Map fieldValue可以理解成Java的接口指针

在早期设计PVO时,我始终认为SQL语言优于EJB的EJBQL语言,优于Hibernate的HQL语言,无论Hibernate的创建者还是使用者怎样的吹捧HQL如何如何的棒,但一个客观的事实是,SQL语言是一种标准的操作数据库的语言,SQL除了在DBMS中使用外,在vb、delphi、php、c/c++、java等等高级语言中均可以使用,它是任何一个软件工作者都无法回避的,是必须掌握的一门基础性语言

核心类ProcessVO提供了一个将每一条记录转换成一个Map对象的方法,通过循环语句将任意的查询结果ResultSet转换成List<Map>类型的变量。因此,JadePool查询操作没有标新立异,它遵守的是行业标准,在语言语法上追求的是道法自然

以下是 _recordMappingToMap的源代码

/**
* 将ResultSet结果集中的记录映射到Map对象中.
*
* @param fieldClassName 是JDBC API中的类型名称,
* @param fieldName 是字段名,
* @param rs 是一个ResultSet查询结果集,
* @param fieldValue Map对象,用于存贮一条记录.
* @throws SQLException
*/
private void _recordMappingToMap(String fieldClassName, String fieldName, ResultSet rs, Map fieldValue) throws SQLException {
fieldName = fieldName.toLowerCase();

//优先规则:常用类型靠前

if (fieldClassName.equals("java.lang.String")) {
String s = rs.getString(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.lang.Integer")) {
int s = rs.getInt(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);//早期jdk需要包装,jdk1.5后不需要包装
}
} else if (fieldClassName.equals("java.lang.Long")) {
long s = rs.getLong(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.lang.Boolean")) {
boolean s = rs.getBoolean(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.lang.Short")) {
short s = rs.getShort(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.lang.Float")) {
float s = rs.getFloat(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.lang.Double")) {
double s = rs.getDouble(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.sql.Timestamp")) {
java.sql.Timestamp s = rs.getTimestamp(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.sql.Date") || fieldClassName.equals("java.util.Date")) {
java.util.Date s = rs.getDate(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.sql.Time")) {
java.sql.Time s = rs.getTime(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.lang.Byte")) {
byte s = rs.getByte(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, new Byte(s));
}
} else if (fieldClassName.equals("[B") || fieldClassName.equals("byte[]")) {
//byte[]出现在SQL Server中
byte[] s = rs.getBytes(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.math.BigDecimal")) {
BigDecimal s = rs.getBigDecimal(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.lang.Object")
|| fieldClassName.equals("oracle.sql.STRUCT")) {
Object s = rs.getObject(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.sql.Array")
|| fieldClassName.equals("oracle.sql.ARRAY")) {
java.sql.Array s = rs.getArray(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.sql.Clob")) {
java.sql.Clob s = rs.getClob(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else if (fieldClassName.equals("java.sql.Blob")) {
java.sql.Blob s = rs.getBlob(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
} else {//对于其它任何未知类型的处理
Object s = rs.getObject(fieldName);
if (rs.wasNull()) {
fieldValue.put(fieldName, null);
} else {
fieldValue.put(fieldName, s);
}
}

}
说明一点,对于一些极少数未知的数据库供应商特有的Java类类型的数据将以Object类型保存在Map对象中

public List query(String sqlquery, int position, int length, boolean isScrollSenstive) throws SQLException 的源代码,

/**
* 通过一个可滚动结果集获取指定起始位置、指定长度的子结果集,不论isScrollSenstive真否,结果集均设定为只读.
*
* @param sqlquery 是标准查询语句,可以是任意复杂的多表查询语句,但必须是受JDBC API支持的标准查询语句
* @param position 记录起始位置,注意表中记录是从1开始;越界则返回0条记录
* @param length 是指定记录长度,若不够长度,则含position后的全部记录
* @param isScrollSenstive 指定结果集是否敏感
* @return
* 获取查询结果集转化成List对象,每一条记录映射成一个HashMap对象,这个HashMap对象的键名是表中的字段名,或是字段的别名,键值为字段值,键值的类型是字段所对应的JDBC
* API的Java类。若无记录则返回零长度List对象。
* @throws SQLException
*/
public List query(String sqlquery, int position, int length, boolean isScrollSenstive) throws SQLException {
List records = new ArrayList();
try {
java.sql.DatabaseMetaData dmd = con.getMetaData();
if (dmd.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)
|| dmd.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE)) {
if (isScrollSenstive) {
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
} else {
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
}
rs = stmt.executeQuery(sqlquery);
rsmd = rs.getMetaData();
int fieldCount = rsmd.getColumnCount();
rs.last();
int x = rs.getRow();
if (position < 1 || position > x) {
return records; //起始位置越界,则返回0条记录;
}
if (position + length > x) {
length = x - (position - 1); //若起始位置后的记录数小于length,则取起始位置后的全部记录;
}
Map valueMap = null;
if (rs.absolute(position)) {
for (int k = position; k < position + length; k++) {
valueMap = new HashMap();
for (int i = 1; i <= fieldCount; i++) {
String fieldClassName = rsmd.getColumnClassName(i);
String fieldName = rsmd.getColumnName(i);
this._recordMappingToMap(fieldClassName, fieldName, rs, valueMap);
}
records.add(valueMap);
if (!rs.next()) {
break;
}
}
}
} finally {
stmt.close();
}
return records;
}


public List query(String sqlquery, int timeout) throws SQLException源代码

/**
* 查询方法
*
* @param sqlquery 是标准查询语句,可以是任意复杂的多表查询语句,但必须是受JDBC API支持的标准查询语句
* @param timeout 设定查询时限,单位:秒;timeout=0,则查询不受时间限制
* @return 将查询结果ResultSet对象转换成List<Map<String,Object>>类型的结果
* @throws SQLException
*/
public List query(String sqlquery, int timeout) throws SQLException {
List records = new ArrayList();
try {
stmt = con.createStatement();
if (timeout > 0) {
stmt.setQueryTimeout(timeout);
}
rs = stmt.executeQuery(sqlquery);
rsmd = rs.getMetaData();
int fieldCount = rsmd.getColumnCount();
while (rs.next()) {
Map valueMap = new LinkedHashMap();
for (int i = 1; i <= fieldCount; i++) {
String fieldClassName = rsmd.getColumnClassName(i);
String fieldName = rsmd.getColumnName(i);
this._recordMappingToMap(fieldClassName, fieldName, rs, valueMap);
}
records.add(valueMap);
}
rs.close();
} finally {
stmt.close();
}
db.setLastQuerySql(sqlquery);
return records;
}


public List query(String sqlquery) throws SQLException 源代码

/**
* 查询方法
*
* @param sqlquery 是标准查询语句,可以是任意复杂的多表查询语句,但必须是受JDBC API支持的标准查询语句
* @return 将查询结果ResultSet对象转换成List<Map<String,Object>>类型的结果
* @throws SQLException
*/
@Override
public List query(String sqlquery) throws SQLException {
return this.query(sqlquery, 0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: