Copyright © 2005 Scientific Applications. All Rights Reserved.

com.sciapp.table.db
Class DefaultDatabaseTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by com.sciapp.table.db.AbstractDatabaseTableModel
          extended by com.sciapp.table.db.DefaultDatabaseTableModel
All Implemented Interfaces:
CacheableTableModel, DatabaseTableModel, Serializable, TableModel

public class DefaultDatabaseTableModel
extends AbstractDatabaseTableModel

DefaultDatabaseTableModel uses a Connection to connect to a database. The column names and their java types can be specified via a String and a class array respectively. Alternatively, this information can also be provided by a TableModel or a DatabaseTableDefinition.

DefaultDatabaseTableModel uses simple sql select statements to retrieve the database data. A "select count(*) from table" is used to get the row count, while a "limit" keyword is used to retrieve a given row or rows.

The "where" clause is constructed from the FilterTableModel instance, with the help of a SQLFilterConverter, while the "order by" clause from the SortTableModel instance.

DefaultDatabaseTableModel was extensively tested under MYSQL v4.0, 4.1 and 5.0, with complete success. It should work with other databases as well, at least those that support the 'limit' keyword. For databases that do not, you can try turning on cursors, by calling setCursors(true).

See Also:
Serialized Form

Field Summary
protected  Connection c
          The current connection to the database
protected  SQLFilterConverter converter
          utility class that converts a TableFilter to an sql 'where' clause.
protected  boolean cursors
          boolean indicating if a cursor will be used to fetch data from the database.
protected  DatabaseTableDefinition definition
          the database definition.
protected  String joinStatement
          a string representing the table or tables and the criteria (if any) that will be used to fetch the database data.
protected  String orderSQL
          a string which is used to hold the order byclause of the sql statement
protected  String whereSQL
          a string which is used to hold the where clause of the sql statement
 
Fields inherited from class com.sciapp.table.db.AbstractDatabaseTableModel
cache, dbFilter, dbSorter, rangedModel, rowCount
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
DefaultDatabaseTableModel(Connection c, String joinStatement, DatabaseTableDefinition def)
          Constructs a DefaultDatabaseTableModel.
DefaultDatabaseTableModel(Connection c, String table, String[] columns)
          Constructs a DefaultDatabaseTableModel.
DefaultDatabaseTableModel(Connection c, String table, String[] columns, Class[] classes)
          Constructs a DefaultDatabaseTableModel.
DefaultDatabaseTableModel(Connection c, String table, TableModel model)
          Constructs a DefaultDatabaseTableModel.
 
Method Summary
 Class getColumnClass(int column)
          Returns Object.class regardless of columnIndex.
 int getColumnCount()
          Returns the number of columns in the model.
 String getColumnName(int column)
          Returns a default name for the column using spreadsheet conventions: A, B, C, ... Z, AA, AB, etc. If column cannot be found, returns an empty string.
 SQLFilterConverter getSQLFilterConverter()
          Returns the object that will be used to convert a TableFilter to an sql 'where' clause.
 void orderByClause()
          This method is called whenever sorting is requested.
 int readRowCount()
          Reads and returns the row count from the underlying data source.
protected  String readRowCountStatement()
          Retrieves the sql statement for the readRowCount method.
 List retrieveRows(int from, int to)
          Fetches multiple rows from the underlying data source.
protected  String retrieveRowsStatement(int from, int to)
          Retrieves the sql statement for the retrieveRows method.
 void setCursors(boolean useCursors)
          Tells DefaultDatabaseTreeTableModel to use cursors when fetching a range of rows from the database.
 void setSQLFilterConverter(SQLFilterConverter converter)
          Assigns the object that will be used to convert a TableFilter to an sql 'where' clause.
 void whereClause()
          This method is called whenever filtering is requested.
 
Methods inherited from class com.sciapp.table.db.AbstractDatabaseTableModel
fireTableChanged, getCache, getFilterTableModel, getRowCount, getSortTableModel, getUncachedRows, getValueAt, isCountCached, isRangedModel, isValueCached, retrieveFromDB, retrieveFromDB, setCache, setRangedModel
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, isCellEditable, removeTableModelListener, setValueAt
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, isCellEditable, removeTableModelListener, setValueAt
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, isCellEditable, removeTableModelListener, setValueAt
 

Field Detail

joinStatement

protected String joinStatement
a string representing the table or tables and the criteria (if any) that will be used to fetch the database data.


converter

protected transient SQLFilterConverter converter
utility class that converts a TableFilter to an sql 'where' clause.


definition

protected DatabaseTableDefinition definition
the database definition.


cursors

protected boolean cursors
boolean indicating if a cursor will be used to fetch data from the database.


c

protected transient Connection c
The current connection to the database


whereSQL

protected String whereSQL
a string which is used to hold the where clause of the sql statement


orderSQL

protected String orderSQL
a string which is used to hold the order byclause of the sql statement

Constructor Detail

DefaultDatabaseTableModel

public DefaultDatabaseTableModel(Connection c,
                                 String table,
                                 String[] columns)
Constructs a DefaultDatabaseTableModel.


DefaultDatabaseTableModel

public DefaultDatabaseTableModel(Connection c,
                                 String table,
                                 String[] columns,
                                 Class[] classes)
Constructs a DefaultDatabaseTableModel.


DefaultDatabaseTableModel

public DefaultDatabaseTableModel(Connection c,
                                 String joinStatement,
                                 DatabaseTableDefinition def)
Constructs a DefaultDatabaseTableModel.

Parameters:
c - the database connection
joinStatement - a string epresenting the table or tables and the criteria (if any) that will be used to fetch the database data.
def - the column defintion

DefaultDatabaseTableModel

public DefaultDatabaseTableModel(Connection c,
                                 String table,
                                 TableModel model)
Constructs a DefaultDatabaseTableModel.

Method Detail

getColumnClass

public Class getColumnClass(int column)
Returns Object.class regardless of columnIndex.

Specified by:
getColumnClass in interface TableModel
Specified by:
getColumnClass in class AbstractDatabaseTableModel
Parameters:
column - the column being queried
Returns:
the Object.class

getColumnCount

public int getColumnCount()
Returns the number of columns in the model. A JTable uses this method to determine how many columns it should create and display by default.

Returns:
the number of columns in the model
See Also:
TableModel.getRowCount()

getColumnName

public String getColumnName(int column)
Returns a default name for the column using spreadsheet conventions: A, B, C, ... Z, AA, AB, etc. If column cannot be found, returns an empty string.

Specified by:
getColumnName in interface TableModel
Specified by:
getColumnName in class AbstractDatabaseTableModel
Parameters:
column - the column being queried
Returns:
a string containing the default name of column

getSQLFilterConverter

public SQLFilterConverter getSQLFilterConverter()
Returns the object that will be used to convert a TableFilter to an sql 'where' clause.

Returns:
the assigned sql filter converter.

orderByClause

public void orderByClause()
This method is called whenever sorting is requested. Preferrably, this method should reconstruct the sql select query according to the sorting columns of the internal SortTableModel instance.

Specified by:
orderByClause in class AbstractDatabaseTableModel

readRowCount

public int readRowCount()
Reads and returns the row count from the underlying data source.

Returns:
the row count

readRowCountStatement

protected String readRowCountStatement()
Retrieves the sql statement for the readRowCount method.

Returns:
the sql statement as a string

retrieveRows

public List retrieveRows(int from,
                         int to)
Fetches multiple rows from the underlying data source. The following condition must be true: from >= to. The List returned must contain the row elements starting at from. However, the List may contain less elements than those specified in the range.

Parameters:
from - the starting row
to - the ending row
Returns:
the retrieved rows

retrieveRowsStatement

protected String retrieveRowsStatement(int from,
                                       int to)
Retrieves the sql statement for the retrieveRows method.

Parameters:
from - the starting row
to - the ending row
Returns:
the sql statement as a string

setCursors

public void setCursors(boolean useCursors)
Tells DefaultDatabaseTreeTableModel to use cursors when fetching a range of rows from the database.

Parameters:
useCursors - true if cursors are to be used.

setSQLFilterConverter

public void setSQLFilterConverter(SQLFilterConverter converter)
Assigns the object that will be used to convert a TableFilter to an sql 'where' clause.

Parameters:
converter - the sql filter converter to assign.

whereClause

public void whereClause()
This method is called whenever filtering is requested. Preferrably, this method should reconstruct the sql select query according to the table filter of the internal FilterTableModel instance.

Specified by:
whereClause in class AbstractDatabaseTableModel

Copyright © 2005 Scientific Applications. All Rights Reserved.