Java SQL ResultSet Read getAllRow(ResultSet rs)

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

Description

Return all rows in String[][] based on result set

License

Open Source License

Parameter

Parameter Description
rs the ResultSet

Exception

Parameter Description
SQLException an exception

Return

number of results

Declaration

private static String[][] getAllRow(ResultSet rs) throws SQLException 

Method Source Code

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

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

import java.util.ArrayList;

public class Main {
    /**/*from   ww  w .jav a  2s . c om*/
     * Return all rows in String[][] based on result set 
     * @param rs the ResultSet
     * @return number of results
     * @throws SQLException
     */
    private static String[][] getAllRow(ResultSet rs) throws SQLException {
        return getAllRow(rs, -1);
    }

    /**
     * Return all rows in String[][] based on result set 
     * @param rs the ResultSet
     * @param row_num number of rows in ResultSet to be used (if less than total, only those will be considered; if more than total, use all)
     * @return string matrix, containing table (row[0] contains column names)
     * @throws SQLException
     */
    private static String[][] getAllRow(ResultSet rs, int row_num) throws SQLException {
        if (rs == null)
            return null;

        ArrayList<String[]> lista = new ArrayList<String[]>();
        final String[] fields = getFieldNames(rs);
        lista.add(fields);

        for (int i = 1; rs.next() && (row_num < 0 || i <= row_num); i++) {
            String[] a = new String[fields.length];

            for (int j = 0; j < fields.length; j++) {
                final String result = rs.getString(j + 1);
                a[j] = result == null ? "" : result.replace("\r", "").replace("\n", " - ").trim();
            }

            lista.add(a);
        }

        return lista.toArray(new String[lista.size()][]);
    }

    /**
     * Return field names
     * @param rs ResultSet to be examinated
     * @return array containing field names
     * @throws SQLException
     */
    private static String[] getFieldNames(ResultSet rs) throws SQLException {
        if (rs == null)
            return null;

        // Return db information based on result set
        ResultSetMetaData metaData = rs.getMetaData();

        // Return filed number of table specified in SQL query
        int col_num = metaData.getColumnCount();
        String[] col_names = new String[col_num];

        // Creates array with field names
        for (int i = 0; i < col_num; i++)
            col_names[i] = metaData.getColumnName(i + 1);

        return col_names;
    }
}

Related

  1. get(ResultSet rs, String name)
  2. getAllColumnNames(ResultSet rs)
  3. getAllColumnNamesFromResultSet(ResultSet set)
  4. getAllData(ResultSet rs)
  5. getAllValueMaps(ResultSet input)
  6. getArray(ResultSet resultSet, String columnLabel, Class clazz)
  7. getArray(ResultSet rs, int index)
  8. getArray(ResultSet rs, String columnName, Class cls)