Top Description Fields Constructors Methods
org.springframework.jdbc.core

public Class RowCountCallbackHandler

extends Object
implements RowCallbackHandler
Class Inheritance
All Implemented Interfaces
org.springframework.jdbc.core.RowCallbackHandler
Imports
java.sql.ResultSet, .ResultSetMetaData, .SQLException, org.springframework.jdbc.support.JdbcUtils, org.springframework.lang.Nullable

Implementation of RowCallbackHandler. Convenient superclass for callback handlers. An instance can only be used once.

We can either use this on its own (for example, in a test case, to ensure that our result sets have valid dimensions), or use it as a superclass for callback handlers that actually do something, and will benefit from the dimension information it provides.

A usage example with JdbcTemplate:

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);  // reusable object

RowCountCallbackHandler countCallback = new RowCountCallbackHandler();  // not reusable
jdbcTemplate.query("select * from user", countCallback);
int rowCount = countCallback.getRowCount();
Author
Rod Johnson
Since
May 3, 2001

Field Summary

Modifier and TypeField and Description
private int
columnCount

Columns we've seen so far.

private String[]
columnNames

Indexed from 0.

private int[]
columnTypes

Indexed from 0.

private int
rowCount

Rows we've seen so far.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final int

Returns:

the number of columns in this result set
getColumnCount
()

Return the number of columns in this result set.

public final String[]

Returns:

the names of the columns. Indexed from 0 to n-1.
getColumnNames
()

Return the names of the columns.

public final int[]

Returns:

the types of the columns as java.sql.Types constants. Indexed from 0 to n-1.
getColumnTypes
()

Return the types of the columns as java.sql.Types constants Valid after processRow is invoked the first time.

public final int

Returns:

the number of rows in this ResultSet
getRowCount
()

Return the row count of this ResultSet.

public final void
processRow(ResultSet
the ResultSet to process (pre-initialized for the current row)
rs
)

Implements org.springframework.jdbc.core.RowCallbackHandler.processRow.

Implementation of ResultSetCallbackHandler.
protected void
processRow(ResultSet
the ResultSet to extract data from. This method is invoked for each row
rs
,
int
number of the current row (starting from 0)
rowNum
)

Subclasses may override this to perform custom extraction or processing.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

columnCountback to summary
private int columnCount

Columns we've seen so far.

columnNamesback to summary
private String[] columnNames

Indexed from 0. Column name as returned by ResultSetMetaData object.

Annotations
@Nullable
columnTypesback to summary
private int[] columnTypes

Indexed from 0. Type (as in java.sql.Types) for the columns as returned by ResultSetMetaData object.

Annotations
@Nullable
rowCountback to summary
private int rowCount

Rows we've seen so far.

Constructor Detail

RowCountCallbackHandlerback to summary
public RowCountCallbackHandler()

Method Detail

getColumnCountback to summary
public final int getColumnCount()

Return the number of columns in this result set. Valid once we've seen the first row, so subclasses can use it during processing

Returns:int

the number of columns in this result set

getColumnNamesback to summary
public final String[] getColumnNames()

Return the names of the columns. Valid after processRow is invoked the first time.

Returns:String[]

the names of the columns. Indexed from 0 to n-1.

Annotations
@Nullable
getColumnTypesback to summary
public final int[] getColumnTypes()

Return the types of the columns as java.sql.Types constants Valid after processRow is invoked the first time.

Returns:int[]

the types of the columns as java.sql.Types constants. Indexed from 0 to n-1.

Annotations
@Nullable
getRowCountback to summary
public final int getRowCount()

Return the row count of this ResultSet. Only valid after processing is complete

Returns:int

the number of rows in this ResultSet

processRowback to summary
public final void processRow(ResultSet rs) throws SQLException

Implements org.springframework.jdbc.core.RowCallbackHandler.processRow.

Implementation of ResultSetCallbackHandler. Work out column size if this is the first row, otherwise just count rows.

Subclasses can perform custom extraction or processing by overriding the processRow(ResultSet, int) method.

Parameters
rs:ResultSet

Doc from org.springframework.jdbc.core.RowCallbackHandler.processRow.

the ResultSet to process (pre-initialized for the current row)

Annotations
@Override
Exceptions
SQLException:

Doc from org.springframework.jdbc.core.RowCallbackHandler.processRow.

if an SQLException is encountered getting column values (that is, there's no need to catch SQLException)

See Also
processRow(java.sql.ResultSet, int)
processRowback to summary
protected void processRow(ResultSet rs, int rowNum) throws SQLException

Subclasses may override this to perform custom extraction or processing. This class's implementation does nothing.

Parameters
rs:ResultSet

the ResultSet to extract data from. This method is invoked for each row

rowNum:int

number of the current row (starting from 0)