Java SQL ResultSet to toMapList(ResultSet rs)

Here you can find the source of toMapList(ResultSet rs)

Description

to Map List

License

Apache License

Declaration

public static List<Map<String, Object>> toMapList(ResultSet rs) throws java.sql.SQLException 

Method Source Code

//package com.java2s;
/**/* w w w  . j  ava2  s  .co  m*/
 * Copyright (C) 2016 - 2017 youtongluan.
 *
 * 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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static List<Map<String, Object>> toMapList(ResultSet rs) throws java.sql.SQLException {
        List<Map<String, Object>> list = new ArrayList<>();
        if (rs == null) {
            return list;
        }
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount();
        Map<String, Object> rowData = new HashMap<>();
        while (rs.next()) {
            rowData = new HashMap<>(columnCount);
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), rs.getObject(i));
            }
            list.add(rowData);
        }
        return list;
    }
}

Related

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