Tag Archives: attempting to get column ‘XX’ from result set

[Solved] Mybatis Error: attempting to get column ‘XX’ from result set

Summary of common causes
1. The field name in the encapsulation set is inconsistent with the database column name. Check whether it is consistent and whether there is this field
2. Using Lombok or other operations results in no parameter free structure or no get/set
3. Use Druid, because the version problem leads to exception handling for the time type localdatetime.class; This solution: do not use druid or use other database connection sources for replacement or Druid version upgrade.
Let’s talk about the source of this exception first:
getResult in basetypehandler.java

  public T getResult(ResultSet rs, String columnName) throws SQLException {
    try {
      // Map database fields and entity class attributes in the result set
      return getNullableResult(rs, columnName);
    } catch (Exception e) {
      throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set.  Cause: " + e, e);
    }
  }

There will be exceptions before this exception, that is, the exception will be caused by many reasons, which means there will be many solutions for the exception. Here is the processing and positioning process of the third exception handling scheme.
problem scenario description
Query the qualified course record information by date. The time field in the entity class is starttime (Format: yyyy MM DD HH: mm: SS), and the field in the database is start_ Time, field type datetime.
Stack exception information (some stack information is deleted due to length reason):

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set.  Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set.  Cause: java.sql.SQLFeatureNotSupportedException
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
Caused by: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set.  Cause: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set.  Cause: java.sql.SQLFeatureNotSupportedException
	at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:87)
	... 73 more
Caused by: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'start_time' from result set.  Cause: java.sql.SQLFeatureNotSupportedException
	at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:85)
	... 99 more
Caused by: java.sql.SQLFeatureNotSupportedException
	at com.alibaba.druid.pool.DruidPooledResultSet.getObject(DruidPooledResultSet.java:1771)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.ibatis.logging.jdbc.ResultSetLogger.invoke(ResultSetLogger.java:69)
	at com.sun.proxy.$Proxy190.getObject(Unknown Source)
	at org.apache.ibatis.type.LocalDateTimeTypeHandler.getNullableResult(LocalDateTimeTypeHandler.java:38)
	at org.apache.ibatis.type.LocalDateTimeTypeHandler.getNullableResult(LocalDateTimeTypeHandler.java:28)
	at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:85)
	... 101 more

Look at the stack information from the beginning. Obviously, it is the exception thrown by getnullableresult in localdatetimetypehandler.java.The earliest exception information: java.sql.sqlfeaturenotsupportedexception.
first look at getnullableresult in localdatetimetypehandler.java

  public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
    return rs.getObject(columnName, LocalDateTime.class);
  }

 rs.getobject has many implementation methods. Because the Druid database connection source is used (the original version is 1.0.28), you can directly locate GetObject in druidpooledresultset.java:

 public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

Looking through the source code according to druid1.0.28, it is found that localdatetime.Class about date type processing instructions are not supported, and sqlfeaturenotsupportedexception is thrown directly. Modify the Druid version in pom.xml, upgrade to 1.2.1, and then take a look at the implementation of this version:

  public <T> T getObject(String columnLabel, Class<T> type) throws SQLException {
        try {
            return this.rs.getObject(columnLabel, type);
        } catch (Throwable var4) {
            throw this.checkException(var4);
        }
    }

Note: the higher version supports localdatetime.Class processing. It is normal after retesting!