import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import javax.swing.table.AbstractTableModel;
/**
* RSTableModel
* @author Worthy Sizemore
* @version November 9, 2007
*
* Creates an AbstractTableModel
designed specifically
* to take information from a ResultSet
for
* display in a JTable
. Calculations for data retrieval
* are based on the fact that a JTable
starts counting its
* rows and columns from 0 while a ResultSet
starts counting
* its records and fields from 1.
*/
@SuppressWarnings("serial")
class RSTableModel extends AbstractTableModel
{
/** The ResultSetMetaData
for the passed ResultSet
*/
private ResultSetMetaData metadata;
/** The number of records that will be displayed. */
private int numberOfRows;
/** The number of columns that will be displayed. */
private int numberOfColumns;
/** The records retrieved from the ResultSet
. */
private Object[][] data;
/** The passed ResultSet
which provides the record data. */
private ResultSet dataSource;
/**
* Parameterized constructor. Initializes all variables based on the
* passed ResultSet
.
*
* @param rs ResultSet
that is to be displayed in a table.
* @exception prints stack trace on any exception
*/
public RSTableModel(ResultSet rs)
{
try
{
dataSource = rs;
metadata = dataSource.getMetaData();
dataSource.last();
numberOfRows = dataSource.getRow();
numberOfColumns = metadata.getColumnCount();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* Retrieves how many columns are needed for the table.
*
* @return numberOfColumns
*/
public int getColumnCount()
{
return numberOfColumns;
}
/**
* Retrieves how many rows are needed for the table.
*
* @return numberOfRows
*/
public int getRowCount()
{
return numberOfRows;
}
/**
* Retrieves the value of a specific cell.
*
* @param row Row position of the cell
* @param col Column position of the cell
* @return value stored in that position or an empty
* String
.
* @exception prints stack trace on any exception
*/
public Object getValueAt(int row, int col)
{
try
{
dataSource.absolute(row + 1);
return dataSource.getObject(col + 1);
}
catch (Exception e)
{
e.printStackTrace();
}
return ""; // If it fails, return empty object.
}
/**
* Retrieves the name of the class type for a specific
* column.
*
* @param col Column whose class type needs to be retrieve.
* @return name of the column's class type or simply class Object
* @exception prints stack trace on any exception
*/
@SuppressWarnings("unchecked")
public Class getColumnClass(int col)
{
try
{
String classname = metadata.getColumnClassName(col + 1);
return Class.forName(classname);
}
catch (Exception e)
{
e.printStackTrace();
}
return Object.class;
}
/**
* Changes a value in a specific cell. This can be used
* to overwrite or even add to the table.
*
* @param value New cell value.
* @param row Row position of the cell to change.
* @param col Column position of the cell to change.
*/
public void setValueAt(Object value, int row, int col)
{
data[row][col] = value;
fireTableCellUpdated(row, col);
}
/**
* Retrieves the field name for a column.
*
* @param col Column number whose field name needs retrieval.
* @return Column name if successful or an empty String
if not.
* @exception prints stack trace on any exception
*/
public String getColumnName(int col)
{
try
{
return metadata.getColumnName(col + 1);
}
catch (Exception e)
{
e.printStackTrace();
}
return "";
}
}