12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 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 查询,返回待类型的
- *
- * <pre>
- *
- * Created by zhaopx.
- * User: zhaopx
- * Date: 2020/4/2
- * Time: 12:12
- *
- * </pre>
- *
- * @author zhaopx
- */
- public class RSTypeHandler extends MapListHandler {
- /**
- * 返回的数据类型
- */
- private List<Map<String, Object>> types = new ArrayList<>();
- @Override
- protected Map<String, Object> 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<String, Object> type = new HashMap<>();
- type.put("name", columnName);
- type.put("type", rsmd.getColumnTypeName(i));
- types.add(type);
- }
- return super.handleRow(rs);
- }
- /**
- * 返回数据类型
- * @return
- */
- public List<Map<String, Object>> getTypes() {
- // 返回不可修改的类型
- return ImmutableList.copyOf(types);
- }
- }
|