Java SQL ResultSet Read getMap(ResultSet resultSet)

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

Description

Returns next record of result set as a Map.

License

Open Source License

Parameter

Parameter Description
resultSet The ResultSet to process.

Declaration

public static Map getMap(ResultSet resultSet) throws SQLException 

Method Source Code

//package com.java2s;
//  it under the terms of the GNU General Public License as published by

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

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**//from w  w  w .jav  a 2  s . co  m
     * Returns next record of result set as a Map.
     * The keys of the map are the column names,
     * as returned by the metadata.
     * The values are the columns as Objects.
     *
     * @param resultSet The ResultSet to process.
     * @exception SQLException if an error occurs.
     */
    public static Map getMap(ResultSet resultSet) throws SQLException {

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

        // Create hashmap, sized to number of columns
        HashMap row = new HashMap(cols, 1);

        // Transfer record into hashmap
        if (resultSet.next()) {
            for (int i = 1; i <= cols; i++) {
                row.put(metaData.getColumnName(i), resultSet.getObject(i));
            }
        } // end while

        return ((Map) row);

    }
}

Related

  1. getListFromRS(Class clazz, ResultSet rs)
  2. getListMapFromResultSet(ResultSet rs)
  3. getLocalDate(ResultSet res, String name)
  4. getLocalDate(ResultSet rs, String columnName)
  5. getMap(ResultSet resultSet)
  6. getMap(ResultSet rs, ResultSetMetaData metaData, int cols_len)
  7. getMetaData(ResultSet rs)
  8. getNullable(final ResultSet resultSet, final T value)
  9. getNullableBooleanFromResultSet(ResultSet rset, Enum field)