com.peter.db
Class DBMatrix

java.lang.Object
  |
  +--com.peter.db.DBMatrix
All Implemented Interfaces:
java.io.Serializable

public class DBMatrix
extends java.lang.Object
implements java.io.Serializable

This class creates a data structure (Matrix) from a ResultSet, in order to that data retrieved from a ResultSet can be used after the ResultSet, and its Statement are no longer needed and can be closed. The data structure used is a HashMap that uses the original ResultSet column names as keys, and Vectors containing data objects as values.

Version:
$Revision: 1.0.1, 2003-11-04 $
Author:
Peter Larsson
See Also:
Serialized Form

Constructor Summary
DBMatrix(java.sql.ResultSet rs)
          The constructor creates an instance of the structure given a ResultSet.
 
Method Summary
 java.util.Vector getColumn(int column)
          Returns a Vector containing all values in a column
 java.util.Vector getColumn(java.lang.String column)
          Returns a Vector containing all values in a column
 java.util.Vector getColumnNames()
          Returns a Vector containing all column names
 java.util.HashMap getData()
          Returns the whole data structure.
 int getNoOfColumns()
          Returns the number of columns of the matrix
 int getNoOfRows()
          Returns the number of rows
 java.util.HashMap getRowAsHashMap(int row)
          Returns a HashMap that contains column names as key, and row data as values, given a row number.
 java.lang.Object getValue(int row, int column)
          Returns a single value from the structure given a row number and a column number.
 java.lang.Object getValue(int row, java.lang.String columnName)
          Returns a single value from the structure given a row number and a column name.
 boolean isEmpty()
          Checks if the matrix is empty or not.
 java.lang.String toString()
          A textual representation of the matrix
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DBMatrix

public DBMatrix(java.sql.ResultSet rs)
         throws java.sql.SQLException
The constructor creates an instance of the structure given a ResultSet.
Parameters:
rs - The ResultSet to convert.
Throws:
java.sql.SQLException - if there is a SQL error
Method Detail

getRowAsHashMap

public java.util.HashMap getRowAsHashMap(int row)
Returns a HashMap that contains column names as key, and row data as values, given a row number.
Parameters:
row - The row to retrieve values from.
Returns:
Returns a HashMap with column names as keys, and row data as values. Returns null if row is bigger than the number of rows in the structure.

getValue

public java.lang.Object getValue(int row,
                                 int column)
Returns a single value from the structure given a row number and a column number.
Parameters:
row - The row index
column - The column index
Returns:
An Object value from the structure

getValue

public java.lang.Object getValue(int row,
                                 java.lang.String columnName)
Returns a single value from the structure given a row number and a column name.
Parameters:
row - The row index
columnName - The name of the column
Returns:
A single value from the structure

getData

public java.util.HashMap getData()
Returns the whole data structure.
Returns:
The whole data structure.

getNoOfColumns

public int getNoOfColumns()
Returns the number of columns of the matrix
Returns:
Returns the number of columns of the matrix

getNoOfRows

public int getNoOfRows()
Returns the number of rows
Returns:
The number of rows in the matrix

getColumnNames

public java.util.Vector getColumnNames()
Returns a Vector containing all column names
Returns:
a Vector containing all column names

getColumn

public java.util.Vector getColumn(int column)
Returns a Vector containing all values in a column
Parameters:
column - The column index to return
Returns:
The data in the requested column

getColumn

public java.util.Vector getColumn(java.lang.String column)
Returns a Vector containing all values in a column
Parameters:
column - The column name to return
Returns:
The data in the requested column

isEmpty

public boolean isEmpty()
Checks if the matrix is empty or not.
Returns:
True if the matrix does not contain any rows, or false otherwise

toString

public java.lang.String toString()
A textual representation of the matrix
Overrides:
toString in class java.lang.Object
Returns:
A textual representation of the matrix