Java SQL ResultSet to toMap(final ResultSet resultSet)

Here you can find the source of toMap(final ResultSet resultSet)

Description

to Map

License

Open Source License

Parameter

Parameter Description
resultSet The ResultSet whose parameters are to be used to populate bean properties

Return

Return a HashMap of the records in a resultSet as a contiguous list.

Declaration

public static HashMap toMap(final ResultSet resultSet) throws SQLException 

Method Source Code

//package com.java2s;
/**//from  w w  w . j a  va2 s  . c  o m
 * Copyright (C) 2008-2010, Squale Project - http://www.squale.org
 *
 * This file is part of Squale.
 *
 * Squale 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 3 of the
 * License, or any later version.
 *
 * Squale 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Squale.  If not, see <http://www.gnu.org/licenses/>.
 */

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

import java.util.HashMap;

public class Main {
    /**
     * @return Return a HashMap of the records in a resultSet as a contiguous list.
     * @param resultSet The ResultSet whose parameters are to be used to populate bean properties
     * @exception SQLException if an exception is thrown while setting property values or access the ResultSet
     */
    public static HashMap toMap(final ResultSet resultSet) throws SQLException {
        // Build a list of relevant column properties from this resultSet
        final HashMap properties = new HashMap();

        // Acquire resultSet MetaData
        final ResultSetMetaData metaData = resultSet.getMetaData();
        final int cols = metaData.getColumnCount();

        // Scroll to next record and pump into hashmap
        while (resultSet.next()) {
            for (int i = 1; i <= cols; i++) {
                properties.put(metaData.getColumnName(i), resultSet.getString(i));
            }
        }

        return (properties);
    }
}

Related

  1. resultSetToXML(ResultSet rs)
  2. toArrayList(ResultSet rs)
  3. toAttributeMap(ResultSet resultSet)
  4. toJSONFromResultSet(ResultSet resultSet)
  5. toListMap(int limit, ResultSet rs)
  6. toMap(final ResultSet resultSet)
  7. toMapList(ResultSet rs)
  8. toMapOfLists(ResultSet rs)
  9. toObjectArray(ResultSet resultSet)