RSTypeHandler.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package com.primeton.dsp.dataservice.utils;
  2. import com.google.common.collect.ImmutableList;
  3. import org.apache.commons.dbutils.handlers.MapListHandler;
  4. import java.sql.ResultSet;
  5. import java.sql.ResultSetMetaData;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. /**
  12. *
  13. * SQL 查询,返回待类型的
  14. *
  15. * <pre>
  16. *
  17. * Created by zhaopx.
  18. * User: zhaopx
  19. * Date: 2020/4/2
  20. * Time: 12:12
  21. *
  22. * </pre>
  23. *
  24. * @author zhaopx
  25. */
  26. public class RSTypeHandler extends MapListHandler {
  27. /**
  28. * 返回的数据类型
  29. */
  30. private List<Map<String, Object>> types = new ArrayList<>();
  31. @Override
  32. protected Map<String, Object> handleRow(ResultSet rs) throws SQLException {
  33. ResultSetMetaData rsmd = rs.getMetaData();
  34. int cols = rsmd.getColumnCount();
  35. for (int i = 1; i <= cols; i++) {
  36. String columnName = rsmd.getColumnLabel(i);
  37. if (null == columnName || 0 == columnName.length()) {
  38. columnName = rsmd.getColumnName(i);
  39. }
  40. Map<String, Object> type = new HashMap<>();
  41. type.put("name", columnName);
  42. type.put("type", rsmd.getColumnTypeName(i));
  43. types.add(type);
  44. }
  45. return super.handleRow(rs);
  46. }
  47. /**
  48. * 返回数据类型
  49. * @return
  50. */
  51. public List<Map<String, Object>> getTypes() {
  52. // 返回不可修改的类型
  53. return ImmutableList.copyOf(types);
  54. }
  55. }