Query interface



public abstract interface Query

Interface to a query used or created by a custom tag. A query contains tabular data organized by named columns and rows.

Methods

Returns

Method

Description

int

addRow()

Adds a row to the query

int

getColumnIndex(String name)

Gets the index of a column given its name

String[]

getColumns()

Gets a list of the column names in a query

String

getData(int iRow, int iCol)

Gets a data element from a row and column of a query

String

getName()

Gets the name of a query

int

getRowCount()

Gets the number of rows in a query

void

setData(int iRow, int iCol, String data)

Sets a data element in a row and column of a query

addRow

Description

Adds a row to a query. Call this method to append a row to a query.

Returns the index of the row that was appended to the query.

Syntax

public int addRow()

See also

setData, getData

Example

The following example demonstrates the addition of two rows to a query that has three columns, City, State, and Zip:

// Define column indexes 
int iCity = 1, iState = 2, iZip = 3 ; 
 
// First row 
int iRow = query.addRow() ; 
query.setData( iRow, iCity, "Minneapolis" ) ; 
query.setData( iRow, iState, "MN" ) ; 
query.setData( iRow, iZip, "55345" ) ; 
// Second row 
iRow = query.addRow() ; 
query.setData( iRow, iCity, "St. Paul" ) ; 
query.setData( iRow, iState, "MN" ) ; 
query.setData( iRow, iZip, "55105" ) ; 

getColumnIndex

Description

Returns the index of the column, or 0 if no such column exists.

Syntax

public int getColumnIndex(String name)

Parameters

Parameter

Description

name

Name of column to get index of (lookup is case-insensitive)

Example

The following example retrieves the index of the EMAIL column and uses it to output a list of the addresses contained in the column:

// Get the index of the EMAIL column 
int iEMail = query.getColumnIndex( "EMAIL" ) ; 
 
// Iterate over the query and output list of addresses 
int nRows = query.getRowCount() ; 
for( int iRow = 1; iRow <= nRows; iRow++ ) 
{ 
    response.write( query.getData( iRow, iEMail ) + "<BR>" ) ; 
} 

getColumns

Description

Returns an array of strings containing the names of the columns in the query.

Syntax

public String[] getColumns()

Example

The following example retrieves the array of columns, then iterates over the list, writing each column name back to the user:

// Get the list of columns from the query 
String[] columns = query.getColumns() ; 
int nNumColumns = columns.length ;                     
 
// Print the list of columns to the user 
response.write( "Columns in query: " ) ; 
for( int i=0; i<nNumColumns; i++ ) 
{ 
    response.write( columns[i] + " " ) ; 
} 

getData

Description

Retrieves a data element from a row and column of a query. Row and column indexes begin with 1. You can find the number of rows in a query by calling getRowCount. You can find the number of columns in a query by calling getColumns.

Returns the value of the requested data element.

Syntax

public String getData(int iRow, int iCol)

Throws

IndexOutOfBoundsException if an invalid index is passed to the method.

See also

setData, addRow

Parameters

Parameter

Description

iRow

Row to retrieve data from (1-based)

iCol

Column to retrieve data from (1-based)

Example

The following example iterates over the rows of a query and writes the data back to the user in a simple, space-delimited format:

int iRow, iCol ; 
int nNumCols = query.getColumns().length ; 
int nNumRows = query.getRowCount() ; 
for ( iRow = 1; iRow <= nNumRows; iRow++ ) 
{ 
    for ( iCol = 1; iCol <= nNumCols; iCol++ ) 
    { 
        response.write( query.getData( iRow, iCol ) + " " ) ; 
    } 
    response.write( "<BR>" ) ; 
} 

getName

Description

Returns the name of a query.

Syntax

public String getName()

Example

The following example retrieves the name of a query and writes it back to the user:

Query query = request.getQuery() ; 
    response.write( "The query name is: " + query.getName() ) ; 

getRowCount

Description

Retrieves the number of rows in a query.

Returns the number of rows contained in a query.

Syntax

public int getRowCount()

Example

The following example retrieves the number of rows in a query and writes it back to the user:

Query query = request.getQuery() ; 
    int rows = query.getRowCount() ; 
    response.write( "The number of rows in the query is "  
    + Integer.ToString(rows) ) ; 

setData

Description

Sets a data element in a row and column of a query. Row and column indexes begin with 1. Before calling setData for a given row, call addRow and use the return value as the row index for your call to setData.

Syntax

public void setData(int iRow, int iCol, String data)

Throws

IndexOutOfBoundsException if an invalid index is passed to the method.

See also

getData, addRow

Parameters

Parameter

Description

iRow

Row of data element to set (1-based)

iCol

Column of data element to set (1-based)

data

New value for data element

Example

The following example demonstrates the addition of two rows to a query that has three columns, City, State, and Zip:

// Define column indexes 
int iCity = 1, iState = 2, iZip = 3 ; 
 
// First row 
int iRow = query.addRow() ; 
query.setData( iRow, iCity, "Minneapolis" ) ; 
query.setData( iRow, iState, "MN" ) ; 
query.setData( iRow, iZip, "55345" ) ; 
 
 
// Second row 
iRow = query.addRow() ; 
query.setData( iRow, iCity, "St. Paul" ) ; 
query.setData( iRow, iState, "MN" ) ; 
query.setData( iRow, iZip, "55105" ) ;