com.bixuebihui.jdbc.RowMapperResultReaderToMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of c-dbtools Show documentation
Show all versions of c-dbtools Show documentation
a fast small database connection pool and a active record flavor mini framework
package com.bixuebihui.jdbc;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
/**
* RowMapperResultReaderToMap class.
*
* @author xingwx
* @version $Id: $Id
*/
public class RowMapperResultReaderToMap {
private final RowMapper handle;
private final String keyName;
private Map res;
/**
* Constructor for RowMapperResultReaderToMap.
*
* @param handle a {@link RowMapper} object.
* @param keyName a {@link java.lang.String} object.
*/
public RowMapperResultReaderToMap(RowMapper handle, String keyName){
this.handle = handle;
this.keyName = keyName;
}
/**
* Constructor for RowMapperResultReaderToMap.
*
* @param handle a {@link RowMapper} object.
* @param keyName a {@link java.lang.String} object.
* @param resultMap a {@link java.util.Map} object.
*/
public RowMapperResultReaderToMap(RowMapper handle, String keyName, Map resultMap){
this.handle = handle;
this.keyName= keyName;
this.res = resultMap;
}
/**
* processResultSet.
*
* @param rs a {@link java.sql.ResultSet} object.
* @return a {@link java.util.Map} object.
* @throws java.sql.SQLException if any.
*/
@SuppressWarnings("unchecked")
public Map processResultSet(ResultSet rs) throws SQLException {
if(res ==null) {
res = new HashMap<>();
}else{
res.clear();
}
int index=1;
while (rs.next()) {
res.put((K)rs.getObject(keyName), handle.mapRow(rs, index++, null));
}
return res;
}
}