Java SQL ResultSet Read getRecordsFromResultSet(ResultSet rs)

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

Description

get Records From Result Set

License

Open Source License

Parameter

Parameter Description
conn An existing SQL Connection
fromSchema The schema containing the table to perform the SELECT statement on.
fromTable The table to perform the SELECT statement on.
whereParams A map of column names to String values used to construct a WHERE clause.

Exception

Parameter Description
SQLException If the query fails.

Return

The resulting rows returned by the query.

Declaration

public static List<Map<String, String>> getRecordsFromResultSet(ResultSet rs) throws SQLException 

Method Source Code

//package com.java2s;
/*/*  ww  w .ja  v a 2 s  . com*/
Weave (Web-based Analysis and Visualization Environment)
Copyright (C) 2008-2011 University of Massachusetts Lowell
    
This file is a part of Weave.
    
Weave is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License, Version 3,
as published by the Free Software Foundation.
    
Weave 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 General Public License
along with Weave.  If not, see <http://www.gnu.org/licenses/>.
*/

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

import java.util.HashMap;

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

import java.util.Vector;

public class Main {
    /**
     * @param conn An existing SQL Connection
     * @param fromSchema The schema containing the table to perform the SELECT statement on.
     * @param fromTable The table to perform the SELECT statement on.
     * @param whereParams A map of column names to String values used to construct a WHERE clause.
     * @return The resulting rows returned by the query.
     * @throws SQLException If the query fails.
     */
    public static List<Map<String, String>> getRecordsFromResultSet(ResultSet rs) throws SQLException {
        // list the column names in the result
        String[] columnNames = new String[rs.getMetaData().getColumnCount()];
        for (int i = 0; i < columnNames.length; i++)
            columnNames[i] = rs.getMetaData().getColumnName(i + 1);
        // create a Map from each row
        List<Map<String, String>> records = new Vector<Map<String, String>>();
        while (rs.next()) {
            Map<String, String> record = new HashMap<String, String>(columnNames.length);
            for (int i = 0; i < columnNames.length; i++) {
                String columnName = columnNames[i];
                String columnValue = rs.getString(columnName);
                record.put(columnName, columnValue);
            }
            records.add(record);
        }
        return records;
    }
}

Related

  1. getObject(ResultSet set, int columnIndex)
  2. getObjectByTypeCoercion(ResultSet resultSet, int index, int dataType)
  3. getOjbClassName(ResultSet rs)
  4. getOptionalInt(ResultSet rs, String name)
  5. getPath(ResultSet r, String columnName)
  6. getResult(ResultSet rs)
  7. getResultArray(ResultSet resultSet, int size, java.util.Date startDate)
  8. getResultByMap(ResultSet rs)
  9. getResults(ResultSet result)