Java SQL ResultSet parseSqlResultToListMap(ResultSet rs)

Here you can find the source of parseSqlResultToListMap(ResultSet rs)

Description

parse Sql Result To List Map

License

Apache License

Declaration

@SuppressWarnings("unchecked")
private static <T> List<T> parseSqlResultToListMap(ResultSet rs) 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.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Main {

    @SuppressWarnings("unchecked")
    private static <T> List<T> parseSqlResultToListMap(ResultSet rs) throws Exception {
        List<Object> list = new ArrayList<Object>();
        ResultSetMetaData metaData = rs.getMetaData();
        int cols_len = metaData.getColumnCount();
        while (rs.next()) {
            list.add(getMap(rs, metaData, cols_len));
        }//ww w  . j ava2s  .  c  om
        if (list.size() == 0) {
            return null;
        } else {
            return (List<T>) list;
        }
    }

    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);
        }
        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. isSupportedResultSetType(Connection connection, int resultSetType)
  2. loadListMap(final ResultSet rs)
  3. logCurrentResultSetRow(Log log, String msg, ResultSet rs)
  4. mapResultSet(ResultSet set)
  5. moveToPageNo(ResultSet rs, int pageNo, int pageSize)
  6. putResultSetToMap(final Hashtable map, final ResultSet resultSet, final String... keys)
  7. safeGetString(ResultSet r, String name)
  8. saveResultsetAsFile(ResultSet rs, String filePath, String fileName)
  9. toXML(ResultSet rs)