|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
com.sas.storage.jdbc.JDBCToTableModelAdapter
public class JDBCToTableModelAdapter
The JDBCToTableModelAdapter allows JDBC data sources to be
displayed in any viewer that requires a model of type
javax.swing.table.TableModel
, mainly being a javax.swing.JTable
.
The adapter requires a java.sql.Connection
object and a query statement.
They can be passed in on the constructor or set via methods.
The setConnection()
method will apply the connection object. The
setQueryStatement()
method will apply the SQL query statement. When
non-null values for both connection and query statement have been
applied, the adapter is initialized and ready to use.
The query statement must be a SQL query that is valid for the JDBC data source. This may include a blank statement, if valid.
This adapter works best with JDBC drivers that can create scrollable result sets. Table
viewers usually need to read backwards to support scrolling the viewer. However, this adapter
can support using scrolling viewers with forward-only result sets
(ResultSet.TYPE_FORWARD_ONLY
). That is accomplished by requerying the result set
whenever there is a need to read a row before the current row. There is a performance
penalty for this because the server must be requeried and the adapter re-initializes. If
you want to turn off this feature, call setResultSetRequeryUsed(false)
.
Here an example of how the adapter can be created using the JdbcOdbc driver.
Connection conn = null;
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
conn = java.sql.DriverManager.getConnection("jdbc:odbc:Northwind");
//create the query statement.
String queryStatement ="select * from orders";
//create the Adapter
JDBCTotableModelAdapter adapter = new JDBCToTableModelAdapter(conn, queryStatement);
}
You can edit your JDBC data with the JDBCToTableModelAdapter if the JDBC driver you are using supports updatable result sets. To do this set the readOnly
property on the JDBCToTableModelAdapter to true. Some drivers have specific syntax requirements on the query statement in order to produce
updatable result sets. Check your JDBC driver documentaion for details. Please note that if the JDBC driver used does not support updatable results
sets, or your query statement is not using the proper syntax to produce an updatable result set, then the result set produced by the driver and used by the JDBCToTableModelAdapter
will be read only. Attempts to update a read only result set with the setValueAt
method will throw an IllegalStateException.
Sorting your data can be achieved with the sort
method. This method will add an order by clause with the specified column names onto the query statement
and reinitialize to obtain a sorted result set. If your query statement already contains an order by clause then it is considered pre-sorted and attempting to re-sort
with the sort
method may fail.
It is possible to lose editing capabilities after applying a sort. This is because some JDBC drivers don't allow sorting and editing to function together. Some drivers may create virtual views which are not editable to support the order by clause, or the syntax of the query statement with the added order by clause cannot produce an updatable result set. In these cases it will be necessary to clear the sort before editing the data. Please check your JDBC driver documentation for details.
The adapter can be configured and used within a webAF project. Add a
com.sas.storage.jdbc.JDBCConnection
component to your project. Open the customizer and
specify the values needed for your JDBC connection.These include driver name and URL.
Now open the customizer for the adapter.
Specify the value for JDBC Connection using the top combo box. The drop-down list
will show all JDBC Connection objects that exist in your project.
You can set the properties for query statement, column information used,
autoCommit, and read-only, as well.
Field Summary | |
---|---|
protected java.util.Locale |
formatLocale
The Locale to use when formatting values. |
protected boolean |
formattedDataUsed
Indicates if data returned is formatted. |
protected javax.swing.event.EventListenerList |
listenerList
List of listeners |
protected com.sas.util.transforms.ObjectToStringTransform |
objectToStringTransform
|
static java.lang.String |
RB_KEY
|
protected boolean |
requeryOnNonVisibleChanges
Indicates if the result set should be reinitialized if changes made are not visible in the result set. |
protected boolean |
retrievingFormattedData
|
protected boolean |
sortAllowed
Indicates whether sorting is allowed. |
Fields inherited from class com.sas.storage.jdbc.JDBCRowCountAdapter |
---|
currentMaxRowCount, maxRowsFound, READ_AHEAD_AMOUNT, readAheadAmount, trueRowCountCalculated |
Fields inherited from class com.sas.storage.jdbc.JDBCAdapter |
---|
autoCommit, CLASS_NAME, columnInfoUsed, conn, designTime, displayTransform, forwardOnly, initialized, isRowCountKnown, LABEL, meta, NAME, printWarnings, queryRowCountDisabled, queryStatement, readOnly, result, resultSetRequeryUsed, resultSetType, resultSetUsed, retrieveNumericTypesAsDouble, rowCount, rowCountQueryString, rowNumber, spcs, stmt, trimUsed |
Fields inherited from interface com.sas.table.InsertRowInterface |
---|
DEFAULT_INDEX |
Fields inherited from interface com.sas.util.SortDirectionInterface |
---|
ASCENDING, DESCENDING, NOT_SORTED |
Constructor Summary | |
---|---|
JDBCToTableModelAdapter()
Default constructor called when the adapter is made in a webAF project. |
|
JDBCToTableModelAdapter(java.sql.Connection conn,
java.lang.String queryStatement)
Constructor used to create an initialized adapter. |
|
JDBCToTableModelAdapter(java.sql.ResultSet result)
Constructor used to connect an adapter directly to a previously created ResultSet. |
Method Summary | |
---|---|
void |
addTableModelListener(javax.swing.event.TableModelListener l)
Adds a listener to the list that is notified each time a change to the data model occurs. |
void |
cancelUncommittedRow()
Removes the "fake" row from the model, effectively performing a cancel. |
void |
commitUncommittedRow()
Commits the data in the row to the underlying model. |
void |
deleteRow(int index)
Deletes a row. |
void |
fireCellEvent(int row,
int column)
Fire a specific event for a model signaling that one cell has changed. |
void |
fireModelEvent()
Fire a default event for a model signaling the some aspect of the model has changed. |
void |
fireModelEvent(java.util.EventObject event)
Fire a specific event for a model signaling that some aspect of the model has changed. |
protected void |
fireModelInsertEvent(int startRow,
int endRow)
Fire a insert event for a model signaling that an insert has occurred. |
java.lang.Class |
getColumnClass(int col)
Returns the most specific superclass for all the cell values in the column. |
int |
getColumnCount()
Returns the number of columns in the model. |
java.lang.String |
getColumnInfoUsed()
Returns the column information that is currently being used as the column label. |
java.lang.String |
getColumnName(int index)
Returns the name of the column at columnIndex. |
static com.sas.beans.ExtendedBeanInfo |
getExtendedBeanInfo()
|
java.lang.String |
getFormat(int columnIndex)
Returns the value from the ResultSetMetaData getColumnTypeName method. |
java.util.Locale |
getFormatLocale()
Returns the Locale to format values when the formattedDataUsed property is set to true. |
java.lang.String |
getFormattedValueAt(int rowIndex,
int columnIndex)
Returns the formatted data for the given cell coordinates. |
java.lang.Double |
getRawSASNumericValue(int rowIndex,
int columnIndex)
Return the raw value of a numeric cell in the result set. |
int |
getRowCount()
Returns the number of rows in the model. |
java.lang.String[] |
getSortedColumns()
Returns an array of string that are the column names that have been sorted. |
int[] |
getSortedDirections()
Returns an array of ints that indicate which direction each column in the sortedColumns array has been sorted. |
int |
getUncommittedRowNumber()
Returns the index of the "fake" row. |
java.lang.Class |
getUnformattedColumnClass(int col)
Returns the type of raw data that exists in the given column. |
java.lang.Object |
getUnformattedValueAt(int rowIndex,
int columnIndex)
Returns the unformatted data for the given cell coordinates. |
java.lang.Object |
getValueAt(int row,
int col)
Returns the value for the cell at columnIndex and rowIndex. |
void |
insertUncommittedRow(int row)
Adds a "fake" row to the table model at the specified location. |
boolean |
isAccurateRowCountAvailable()
Returns a boolean value indicating if the row count returned by the getRowCount method is accurate. |
boolean |
isCellEditable(int row,
int col)
Returns true if the cell at rowIndex and columnIndex is editable. |
boolean |
isColumnSortable(java.lang.String columnName)
Returns a boolean indicating whether the columnName is allowed to be sorted. |
boolean |
isFormattedDataUsed()
Returns whether the adapter is returning formatted data or the raw data. |
boolean |
isRequeryOnNonVisibleChanges()
Returns a value indicating if the result set should be reinitialized when non-visible changes are made to the result set. |
boolean |
isRowDeletionAllowed()
Indicates whether rows can be deleted. |
boolean |
isRowInsertionAllowed()
Indicates whether rows can be inserted into the existing table. |
boolean |
isRowUpdateAllowed()
Indicates whether rows can be updated. |
boolean |
isSortAllowed()
Returns a boolean indicating whether sorting is allowed. |
boolean |
isSortColumnNamesQuoted()
|
boolean |
isTableExtendOnly()
Indicates whether rows can only be inserted at the default location (usually at the end) of the existing table. |
boolean |
isUncommittedRowPresent()
Returns true if the "fake" row exists. |
void |
removeTableModelListener(javax.swing.event.TableModelListener l)
Removes a listener from the list that is notified each time a change to the data model occurs. |
protected java.lang.Object |
retrieveValueAt(int col,
java.sql.ResultSet result)
Returns value of a cell in the JDBC data type of the cell. |
void |
setColumnInfoUsed(java.lang.String info)
Sets the column information that is currently being used as the column label. |
void |
setFormatLocale(java.util.Locale locale)
Sets the Locale to format values when the formattedDataUsed property is set to true. |
void |
setFormattedDataUsed(boolean useFormattedData)
Sets whether the adapter returns formatted data or the raw data. |
void |
setQueryStatement(java.lang.String queryStatement)
The queryStatement should be any SQL query that is
valid for the JDBC data source. |
void |
setRequeryOnNonVisibleChanges(boolean requeryOnNonVisibleChanges)
The ability for a ResultSet object to detect insertions and deletions is driver dependant. |
void |
setSortAllowed(boolean sortAllowed)
Sets whether sorting is allowed. |
void |
setSortColumnNamesQuoted(boolean isQuoted)
|
void |
setValueAt(java.lang.Object value,
int rowIndex,
int columnIndex)
Sets the value in the cell at columnIndex and rowIndex to value. |
void |
sort(java.lang.String[] columnNames,
int[] direction)
This method will add an order by clause with the specified column names onto the query statement and reinitialize to obtain a sorted result set. |
Methods inherited from class com.sas.storage.jdbc.JDBCRowCountAdapter |
---|
calculateCurrentMaxRowCount, getReadAheadAmount, isTrueRowCountCalculated, setConnection, setReadAheadAmount, setTrueRowCountCalculated |
Methods inherited from interface com.sas.table.TrueRowCountCalculatedInterface |
---|
isTrueRowCountCalculated, setTrueRowCountCalculated |
Field Detail |
---|
public static final java.lang.String RB_KEY
protected javax.swing.event.EventListenerList listenerList
protected boolean requeryOnNonVisibleChanges
protected java.util.Locale formatLocale
protected com.sas.util.transforms.ObjectToStringTransform objectToStringTransform
protected boolean formattedDataUsed
protected boolean sortAllowed
protected boolean retrievingFormattedData
Constructor Detail |
---|
public JDBCToTableModelAdapter()
public JDBCToTableModelAdapter(java.sql.ResultSet result)
result
- ResultSet to use.public JDBCToTableModelAdapter(java.sql.Connection conn, java.lang.String queryStatement)
conn
- A java.sql.Connection
object.queryStatement
- A SQL query that is valid for the database
represented by conn
.Method Detail |
---|
public void addTableModelListener(javax.swing.event.TableModelListener l)
addTableModelListener
in interface javax.swing.table.TableModel
l
- The TableModelListenerpublic void removeTableModelListener(javax.swing.event.TableModelListener l)
removeTableModelListener
in interface javax.swing.table.TableModel
l
- The TableModelListenerpublic void setValueAt(java.lang.Object value, int rowIndex, int columnIndex)
setValueAt
in interface javax.swing.table.TableModel
value
- The new valuerowIndex
- The row whose value is to be changedcolumnIndex
- The column whose value is to be changed
java.lang.IllegalStateException
- if the result set is not updatablepublic java.lang.String getColumnName(int index)
getColumnName
in interface javax.swing.table.TableModel
index
- The index of the column
public int getColumnCount()
getColumnCount
in interface javax.swing.table.TableModel
public java.lang.Class getColumnClass(int col)
getColumnClass
in interface javax.swing.table.TableModel
col
- The index of the column
public java.lang.Class getUnformattedColumnClass(int col)
getUnformattedColumnClass
in interface com.sas.table.FormattedInterface
columnIndex
- the column whose raw data type is being queried
public int getRowCount()
getRowCount
in interface javax.swing.table.TableModel
public boolean isCellEditable(int row, int col)
isCellEditable
in interface javax.swing.table.TableModel
row
- The row whose value to be queriedcol
- The column whose value to be queried
public java.lang.String getColumnInfoUsed()
NAME
or LABEL
if the field has been set.
public void setColumnInfoUsed(java.lang.String info)
info
- The type of information to use in column labels.public static com.sas.beans.ExtendedBeanInfo getExtendedBeanInfo()
public void fireCellEvent(int row, int column)
fireCellEvent
in class JDBCAdapter
row
- row number of the cell that has been modifiedcolumn
- column number of the cell that has been modifiedpublic void fireModelEvent(java.util.EventObject event)
fireModelEvent
in class JDBCAdapter
event
- Event that must be cast that should be firedpublic void fireModelEvent()
fireModelEvent
in class JDBCAdapter
protected void fireModelInsertEvent(int startRow, int endRow)
JDBCRowCountAdapter
fireModelInsertEvent
in class JDBCRowCountAdapter
public void deleteRow(int index) throws com.sas.util.UncheckedException
Some implementations may not allow rows to be deleted. This can be determined through isRowDeletionAllowed().
deleteRow
in interface DeleteRowInterface
index
- A zero-based index that identifies the row.
com.sas.util.UncheckedException
- Provides a catch-all for all Exceptions.public boolean isRowDeletionAllowed()
isRowDeletionAllowed
in interface DeleteRowInterface
public boolean isRequeryOnNonVisibleChanges()
public void setRequeryOnNonVisibleChanges(boolean requeryOnNonVisibleChanges)
Setting this property to true only affects adapters created with a Connection and a query statement. It will have no affect if the adapter was instantiated with a ResultSet.
The resultSetRequeryUsed
property must also be true for reinitialization to occur.
requeryOnNonVisibleChanges
- indicates if the result set should be reinitialized when non-visible changes are madepublic boolean isAccurateRowCountAvailable()
isAccurateRowCountAvailable
in interface AccurateRowCountInterface
public boolean isRowUpdateAllowed()
isRowUpdateAllowed
in interface UpdateRowInterface
public void insertUncommittedRow(int row)
InsertRowInterface
insertUncommittedRow
in interface InsertRowInterface
row
- The row to add the "fake" row before.public void commitUncommittedRow()
InsertRowInterface
commitUncommittedRow
in interface InsertRowInterface
public void cancelUncommittedRow()
InsertRowInterface
cancelUncommittedRow
in interface InsertRowInterface
public boolean isRowInsertionAllowed()
InsertRowInterface
isRowInsertionAllowed
in interface InsertRowInterface
public boolean isTableExtendOnly()
InsertRowInterface
isTableExtendOnly
in interface InsertRowInterface
public int getUncommittedRowNumber()
InsertRowInterface
getUncommittedRowNumber
in interface InsertRowInterface
public boolean isUncommittedRowPresent()
InsertRowInterface
isUncommittedRowPresent
in interface InsertRowInterface
public void setQueryStatement(java.lang.String queryStatement)
queryStatement
should be any SQL query that is
valid for the JDBC data source. It is used to extract the desired data
from the database.
Calling this method has no affect if the constructer taking a ResultSet was used to create the adapter and the ResultSet is not a RowSet.
setQueryStatement
in class JDBCRowCountAdapter
queryStatement
- The SQL query to apply.public boolean isSortColumnNamesQuoted()
public void setSortColumnNamesQuoted(boolean isQuoted)
public void sort(java.lang.String[] columnNames, int[] direction) throws com.sas.table.SortException
Each columnName must have a matching direction. If not, then a SortException will be thrown.
sort
in interface com.sas.table.SortableInterface
columnNames
- The names of the columns in the order that the columns will be sorted.direction
- The direction in which to sort the columns. Valid values are
SortDirectionInterface.ASCENDING and SortDirectionInterface.DESCENDING.
com.sas.table.SortException
public boolean isColumnSortable(java.lang.String columnName)
isColumnSortable
in interface com.sas.table.SortableInterface
columnName
- The name of the column to query about whether it is allowed to be
sorted.
public boolean isSortAllowed()
isSortAllowed
in interface com.sas.table.SortableInterface
public void setSortAllowed(boolean sortAllowed)
sortAllowed
property to true will
have no affect.
setSortAllowed
in interface com.sas.table.SortableInterface
allowed
- true if sorting is allowed.public java.lang.String[] getSortedColumns()
getSortedColumns
in interface com.sas.table.SortableInterface
public int[] getSortedDirections()
getSortedDirections
in interface com.sas.table.SortableInterface
public java.lang.String getFormat(int columnIndex)
getFormat
in interface com.sas.table.FormattedInterface
columnIndex
- the column index in the model that the format is to be returned for
public java.lang.Object getValueAt(int row, int col)
getValueAt
in interface javax.swing.table.TableModel
row
- The row whose value is to be queriedcol
- The column whose value is to be queried
public java.lang.String getFormattedValueAt(int rowIndex, int columnIndex)
getFormattedValueAt
in interface com.sas.table.FormattedInterface
rowIndex
- the row whose value is to be queriedcolumnIndex
- the column whose value is to be queried
public java.lang.Object getUnformattedValueAt(int rowIndex, int columnIndex)
getUnformattedValueAt
in interface com.sas.table.FormattedInterface
rowIndex
- the row whose value is to be queriedcolumnIndex
- the column whose value is to be queried
protected java.lang.Object retrieveValueAt(int col, java.sql.ResultSet result) throws java.sql.SQLException
JDBCAdapter
retrieveValueAt
in class JDBCAdapter
col
- A one-based index that identifies the cell's column.result
- ResultSet in which to get values.
java.sql.SQLException
- Thrown if ResultSet throws a SQLException.public void setFormattedDataUsed(boolean useFormattedData)
setFormattedDataUsed
in interface com.sas.table.FormattedInterface
useFormattedData
- whether to use formatted data by default versus raw dataisFormattedDataUsed()
public boolean isFormattedDataUsed()
isFormattedDataUsed
in interface com.sas.table.FormattedInterface
setFormattedDataUsed(boolean)
public void setFormatLocale(java.util.Locale locale)
public java.util.Locale getFormatLocale()
public java.lang.Double getRawSASNumericValue(int rowIndex, int columnIndex)
RawSASNumericValueProvider
getRawSASNumericValue
in interface RawSASNumericValueProvider
rowIndex
- the row whose value is to be queriedcolumnIndex
- the column whose value is to be queried
|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |