package com.primeton.dsp.dataservice.utils; import com.google.common.collect.ImmutableList; import org.apache.commons.dbutils.handlers.MapListHandler; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * * SQL 查询,返回待类型的 * *
 *
 * Created by zhaopx.
 * User: zhaopx
 * Date: 2020/4/2
 * Time: 12:12
 *
 * 
* * @author zhaopx */ public class RSTypeHandler extends MapListHandler { /** * 返回的数据类型 */ private List> types = new ArrayList<>(); @Override protected Map handleRow(ResultSet rs) throws SQLException { ResultSetMetaData rsmd = rs.getMetaData(); int cols = rsmd.getColumnCount(); for (int i = 1; i <= cols; i++) { String columnName = rsmd.getColumnLabel(i); if (null == columnName || 0 == columnName.length()) { columnName = rsmd.getColumnName(i); } Map type = new HashMap<>(); type.put("name", columnName); type.put("type", rsmd.getColumnTypeName(i)); types.add(type); } return super.handleRow(rs); } /** * 返回数据类型 * @return */ public List> getTypes() { // 返回不可修改的类型 return ImmutableList.copyOf(types); } }