Java SQL Table Column toMap(ResultSet rs, List wantedColumnNames)

Here you can find the source of toMap(ResultSet rs, List wantedColumnNames)

Description

to Map

License

Open Source License

Declaration

public static final Map toMap(ResultSet rs, List wantedColumnNames) throws SQLException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.util.ArrayList;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Main {

    public static final Map toMap(ResultSet rs, List wantedColumnNames) throws SQLException {
        Map columns = new LinkedHashMap();

        // Set up the map of columns
        int numWantedColumns = wantedColumnNames.size();
        for (int i = 0; i < numWantedColumns; ++i) {
            List columnValues = new ArrayList();
            columns.put(wantedColumnNames.get(i), columnValues);
        }/*from w w w  .ja  v  a 2 s.  c  om*/

        while (rs.next()) {
            for (int i = 0; i < numWantedColumns; ++i) {
                String columnName = (String) wantedColumnNames.get(i);
                Object value = rs.getObject(columnName);
                List columnValues = (List) columns.get(columnName);
                columnValues.add(value);
                columns.put(columnName, columnValues);

            }
        }

        return columns;
    }
}

Related

  1. tableContainsColumn(Connection conn, String table, String column)
  2. tableContainsIndex(Connection conn, String table, String column, String index)
  3. toColumnNameList(final ResultSet rs)
  4. toFlag(ResultSetMetaData metaData, int column)
  5. toFormatList(ResultSet results, List column)
  6. toValuesList(ResultSet results, String column)