Java源码示例:org.springframework.jdbc.IncorrectResultSetColumnCountException

示例1
/**
 * 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;
}
 
示例2
/**
 * 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;
}
 
示例3
/**
 * 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;
}
 
示例4
/**
 * 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")
@Nullable
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;
}
 
示例5
/**
 * 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")
@Nullable
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;
}
 
示例6
@Test(expected = IncorrectResultSetColumnCountException.class)
public void whenRetrievingMultipleColumns_thenIncorrectResultSetColumnCountException() {
    final JdbcTemplate jdbcTemplate = new JdbcTemplate(restDataSource);
    try {
        jdbcTemplate.execute("insert into foo(id,name) values (1,'a')");
        jdbcTemplate.queryForList("select id,name from foo where id=1", Foo.class);
    } finally {
        jdbcTemplate.execute("delete from foo where id=1");
    }
}