Java SQL ResultSet Read getMap(ResultSet rs, ResultSetMetaData metaData, int cols_len)

Here you can find the source of getMap(ResultSet rs, ResultSetMetaData metaData, int cols_len)

Description

get Map

License

Apache License

Declaration

public static Map<String, Object> getMap(ResultSet rs, ResultSetMetaData metaData, int cols_len)
            throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import java.sql.Types;

import java.util.LinkedHashMap;

import java.util.Map;

public class Main {
    public static Map<String, Object> getMap(ResultSet rs, ResultSetMetaData metaData, int cols_len)
            throws Exception {
        Map<String, Object> map = new LinkedHashMap<String, Object>();

        for (int i = 0; i < cols_len; i++) {
            String cols_name = metaData.getColumnLabel(i + 1);
            Object cols_value = getValueByObjectType(metaData, rs, i);
            map.put(cols_name, cols_value);
        }//  w w w. j ava 2s  .  c o m
        return map;
    }

    public static Object getValueByObjectType(ResultSetMetaData metaData, ResultSet rs, int index)
            throws Exception {
        int columnIndex = index + 1;
        Object return_obj = rs.getObject(columnIndex);
        if (return_obj != null) {
            int type = metaData.getColumnType(columnIndex);
            switch (type) {
            case Types.BIT:
                return_obj = rs.getByte(columnIndex);
                break;
            case Types.TINYINT:
                return_obj = rs.getByte(columnIndex);
                break;
            case Types.SMALLINT:
                return_obj = rs.getShort(columnIndex);
                break;
            case Types.LONGVARBINARY:
                return_obj = rs.getBytes(columnIndex);
                break;
            case Types.BLOB:
                return_obj = rs.getBytes(columnIndex);
                break;
            default:
                return_obj = rs.getObject(columnIndex);
            }
        }
        return return_obj;
    }
}

Related

  1. getListMapFromResultSet(ResultSet rs)
  2. getLocalDate(ResultSet res, String name)
  3. getLocalDate(ResultSet rs, String columnName)
  4. getMap(ResultSet resultSet)
  5. getMap(ResultSet resultSet)
  6. getMetaData(ResultSet rs)
  7. getNullable(final ResultSet resultSet, final T value)
  8. getNullableBooleanFromResultSet(ResultSet rset, Enum field)
  9. getNullableByte(ResultSet resultSet, String columnLabel)