Search in sources :

Example 1 with IncorrectResultSetColumnCountException

use of org.springframework.jdbc.IncorrectResultSetColumnCountException in project spring-framework by spring-projects.

the class SingleColumnRowMapper method mapRow.

/**
	 * Extract a value for the single column in the current row.
	 * <p>Validates that there is only one column selected,
	 * then delegates to {@code getColumnValue()} and also
	 * {@code convertValueToRequiredType}, if necessary.
	 * @see java.sql.ResultSetMetaData#getColumnCount()
	 * @see #getColumnValue(java.sql.ResultSet, int, Class)
	 * @see #convertValueToRequiredType(Object, Class)
	 */
@Override
@SuppressWarnings("unchecked")
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
    // Validate column count.
    ResultSetMetaData rsmd = rs.getMetaData();
    int nrOfColumns = rsmd.getColumnCount();
    if (nrOfColumns != 1) {
        throw new IncorrectResultSetColumnCountException(1, nrOfColumns);
    }
    // Extract column value from JDBC ResultSet.
    Object result = getColumnValue(rs, 1, this.requiredType);
    if (result != null && this.requiredType != null && !this.requiredType.isInstance(result)) {
        // Extracted value does not match already: try to convert it.
        try {
            return (T) convertValueToRequiredType(result, this.requiredType);
        } catch (IllegalArgumentException ex) {
            throw new TypeMismatchDataAccessException("Type mismatch affecting row number " + rowNum + " and column type '" + rsmd.getColumnTypeName(1) + "': " + ex.getMessage());
        }
    }
    return (T) result;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) IncorrectResultSetColumnCountException(org.springframework.jdbc.IncorrectResultSetColumnCountException) TypeMismatchDataAccessException(org.springframework.dao.TypeMismatchDataAccessException)

Aggregations

ResultSetMetaData (java.sql.ResultSetMetaData)1 TypeMismatchDataAccessException (org.springframework.dao.TypeMismatchDataAccessException)1 IncorrectResultSetColumnCountException (org.springframework.jdbc.IncorrectResultSetColumnCountException)1