Java SQL ResultSet to toAttributeMap(ResultSet resultSet)

Here you can find the source of toAttributeMap(ResultSet resultSet)

Description

to Attribute Map

License

Open Source License

Declaration

public static Map<String, ?> toAttributeMap(ResultSet resultSet) throws SQLException 

Method Source Code

//package com.java2s;
/*/*  w w  w . ja  va2 s.  c  om*/
 * Copyright 2015 The Solmix Project
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.gnu.org/licenses/ 
 * or see the FSF site: http://www.fsf.org. 
 */

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

    public static Map<String, ?> toAttributeMap(ResultSet resultSet) throws SQLException {
        return toAttributeMap(resultSet, null, true, null, null);
    }

    public static Map<String, ?> toAttributeMap(ResultSet resultSet, ResultSetMetaData rsmd, boolean useColumnLabel,
            Map<String, String> caseInsensitiveMap, List<String> outputs) throws SQLException {
        if (rsmd == null)
            rsmd = resultSet.getMetaData();
        int count = rsmd.getColumnCount();
        Map<String, Object> __return = new HashMap<String, Object>();
        for (int colCursor = 1; colCursor <= count; colCursor++) {
            String columnName;
            if (useColumnLabel)
                columnName = rsmd.getColumnLabel(colCursor);
            else
                columnName = rsmd.getColumnName(colCursor);
            if (caseInsensitiveMap != null && caseInsensitiveMap.get(columnName) != null)
                columnName = caseInsensitiveMap.get(columnName);
            Object obj = resultSet.getObject(colCursor);
            if (outputs != null && !outputs.contains(columnName))
                continue;
            if (obj == null) {
                __return.put(columnName, obj);
                continue;
            }

            __return.put(columnName, obj);
        }
        return __return;
    }
}

Related

  1. resultSetToSet(PreparedStatement statement)
  2. resultSetToString(ResultSet resultSet)
  3. resultSetToString(ResultSet resultSet)
  4. resultSetToXML(ResultSet rs)
  5. toArrayList(ResultSet rs)
  6. toJSONFromResultSet(ResultSet resultSet)
  7. toListMap(int limit, ResultSet rs)
  8. toMap(final ResultSet resultSet)
  9. toMap(final ResultSet resultSet)