Search in sources :

Example 1 with TypeMismatchDataAccessException

use of org.springframework.dao.TypeMismatchDataAccessException in project spring-framework by spring-projects.

the class DataAccessUtilsTests method withDate.

@Test
public void withDate() {
    Date date = new Date();
    Collection<Date> col = new HashSet<>(1);
    col.add(date);
    assertEquals(date, DataAccessUtils.uniqueResult(col));
    assertEquals(date, DataAccessUtils.requiredUniqueResult(col));
    assertEquals(date, DataAccessUtils.objectResult(col, Date.class));
    assertEquals(date.toString(), DataAccessUtils.objectResult(col, String.class));
    try {
        DataAccessUtils.intResult(col);
        fail("Should have thrown TypeMismatchDataAccessException");
    } catch (TypeMismatchDataAccessException ex) {
    // expected
    }
    try {
        DataAccessUtils.longResult(col);
        fail("Should have thrown TypeMismatchDataAccessException");
    } catch (TypeMismatchDataAccessException ex) {
    // expected
    }
}
Also used : TypeMismatchDataAccessException(org.springframework.dao.TypeMismatchDataAccessException) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with TypeMismatchDataAccessException

use of org.springframework.dao.TypeMismatchDataAccessException 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

TypeMismatchDataAccessException (org.springframework.dao.TypeMismatchDataAccessException)2 ResultSetMetaData (java.sql.ResultSetMetaData)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 IncorrectResultSetColumnCountException (org.springframework.jdbc.IncorrectResultSetColumnCountException)1