Java SQL ResultSet Read readResults(ResultSet resultSet)

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

Description

read Results

License

Apache License

Declaration

public static List<Map<String, Object>> readResults(ResultSet resultSet) throws SQLException 

Method Source Code

//package com.java2s;
/*/*  w  w w .j av a  2  s  .c  o  m*/
 * Copyright (C) 2016 Travis Sharp <furiousscissors@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
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 List<Map<String, Object>> readResults(ResultSet resultSet) throws SQLException {
        String[] columns = getColumns(resultSet);
        List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();

        while (resultSet.next()) {
            Map<String, Object> current = new LinkedHashMap<String, Object>();

            for (int i = 0; i < columns.length; i++) {
                Object data = resultSet.getObject(columns[i]);
                if (data != null) {
                    current.put(columns[i].toLowerCase(), resultSet.getObject(columns[i]));
                }
            }

            results.add(current);
        }

        return results;
    }

    public static String[] getColumns(ResultSet resultSet) throws SQLException {
        ResultSetMetaData metadata = resultSet.getMetaData();
        String[] results = new String[metadata.getColumnCount()];

        for (int i = 1; i <= metadata.getColumnCount(); i++) {
            results[i - 1] = metadata.getColumnName(i);
        }

        return results;
    }
}

Related

  1. readBitBoolean(ResultSet result, int index)
  2. readBytes(ResultSet rs, int pos)
  3. readDate(ResultSet result, int index)
  4. readMap(final ResultSet rs)
  5. readObject(java.sql.ResultSet resultSet, int index)
  6. wasNull(ResultSet rs, T value)
  7. wasNull(ResultSet rs, T value)
  8. wasNull(ResultSet rs, T value)