Java SQL ResultSet Read getRow(ResultSet rs)

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

Description


Purpose: get the next row from the ResultSet as a Collection
Assumptions: that rs.next() was already called and is true.

License

Open Source License

Parameter

Parameter Description

Return

List of items in the row, if rs.next() is null, then null

Declaration

public static List getRow(ResultSet rs) throws SQLException 

Method Source Code

//package com.java2s;
/** Copyright (C) (MSA, Inc) All rights reserved.
 ** General Public License: http://www.opensource.org/licenses/gpl-license.php
 **///from  w ww .  j a  v a2 s  . co m

import java.sql.*;
import java.util.*;

public class Main {
    /** <br><em>Purpose:</em> get the next row from the ResultSet as a Collection
     * <br><em>Assumptions:</em>  that rs.next() was already called and is true.
     * This version calls the getRow with the metadata and the column count
     * @param                     rs, ResultSet
     * @return                    List of items in the row, if rs.next() is null, then null
     * @exception                 SQLException
     **/
    public static List getRow(ResultSet rs) throws SQLException {
        ResultSetMetaData meta = rs.getMetaData();
        int numberOfColumns = meta.getColumnCount();
        return getRow(rs, meta, numberOfColumns, null);
    }

    /** <br><em>Purpose:</em> get the next row from the ResultSet as a Collection
     * <br><em>Assumptions:</em>  that rs.next() was already called and is true
     * @param                     rs, ResultSet
     * @param                     meta, ResultSetMetaData (obtained externally via rs.getMetaData())
     * @param                     numberOfColumns, int (obtained externally via meta.getColumnCount())
     * @param                     maxColCount, Integer
     * @return                    List of items in the row, as they are encountered
     * @exception                 SQLException
     **/
    public static List getRow(ResultSet rs, ResultSetMetaData meta, int numberOfColumns, Integer maxColCount)
            throws SQLException {
        List row = new ArrayList(); // can add 'null' to an ArrayList
        String name;
        for (int i = 1; i <= numberOfColumns; i++) { // get each value in the row
            if (maxColCount != null && (i - 1) == maxColCount.intValue())
                break;
            name = meta.getColumnName(i);
            switch (meta.getColumnType(i)) {
            case java.sql.Types.ARRAY:
                row.add(rs.getArray(i));
                break;
            case java.sql.Types.BIGINT:
                row.add(new Long(rs.getLong(i))); //?? not sure if correct
                break;
            case java.sql.Types.BINARY:
                row.add(rs.getBinaryStream(i));
                break;
            case java.sql.Types.BIT:
                row.add(new Byte(rs.getByte(i))); //?? not sure if correct
                break;
            case java.sql.Types.BLOB:
                row.add(rs.getBlob(i));
                break;
            case java.sql.Types.CHAR:
                row.add(rs.getString(i)); //?? not sure if correct
                break;
            case java.sql.Types.CLOB:
                row.add(rs.getClob(i));
                break;
            case java.sql.Types.DATE:
                row.add(rs.getDate(i));
                break;
            case java.sql.Types.DECIMAL:
                row.add(rs.getBigDecimal(i));
                break;
            case java.sql.Types.DISTINCT:
                row.add(null); //?? not sure if correct
                break;
            case java.sql.Types.DOUBLE:
                row.add(new Double(rs.getDouble(i)));
                break;
            case java.sql.Types.FLOAT:
                row.add(new Float(rs.getFloat(i)));
                break;
            case java.sql.Types.INTEGER:
                row.add(new Integer(rs.getInt(i)));
                break;
            case java.sql.Types.JAVA_OBJECT:
                row.add(rs.getObject(i));
                break;
            case java.sql.Types.LONGVARBINARY:
                row.add(new Long(rs.getLong(i))); //?? not sure if correct
                break;
            case java.sql.Types.LONGVARCHAR:
                row.add(rs.getString(i)); //?? not sure if correct
                break;
            case java.sql.Types.NULL:
                row.add(null);
                break;
            case java.sql.Types.NUMERIC:
                row.add(new Double(rs.getDouble(i))); //?? not sure if correct
                break;
            case java.sql.Types.OTHER:
                row.add(null); //?? not sure if correct
                break;
            case java.sql.Types.REAL:
                row.add(new Double(rs.getDouble(i))); //?? not sure if correct
                break;
            case java.sql.Types.REF:
                row.add(rs.getRef(i));
                break;
            case java.sql.Types.SMALLINT:
                row.add(new Short(rs.getShort(i)));
                break;
            case java.sql.Types.STRUCT:
                row.add(null); //?? not sure if correct
                break;
            case java.sql.Types.TIME:
                row.add(rs.getTime(i));
                break;
            case java.sql.Types.TIMESTAMP:
                row.add(rs.getTimestamp(i));
                break;
            case java.sql.Types.TINYINT:
                row.add(new Short(rs.getShort(i))); //?? not sure if correct
                break;
            case java.sql.Types.VARBINARY:
                row.add(rs.getBinaryStream(i)); //?? not sure if correct
                break;
            case java.sql.Types.VARCHAR:
                row.add(rs.getString(i)); //?? not sure if correct
                break;
            default:
                row.add(null); //?? not sure if correct
            }
        }
        return row;
    }
}

Related

  1. getResultSetValue(ResultSet rs, int index)
  2. getResultSetValue(ResultSet rs, int index)
  3. getResultSetValue(ResultSet rs, int index, Class requiredType)
  4. getResultSetValue(ResultSet rs, int index, Class requiredType)
  5. getReturnCountInt(ResultSet rs, int column)
  6. getRowCount(final ResultSet rs)
  7. getRowCount(ResultSet set)
  8. getRows(final ResultSet rs)
  9. getRows(ResultSet table)