JDBC ResultSet 对象类型映射没有 Byte 或 Short?为什么只有整数?

JDBC ResultSet object types mapping has no Byte or Short? Why Integer only?

美好的一天。谁能解释为什么 JDBC 没有为某些类型实现对象映射。例如,Postgres JDBC 没有 Byte 和 Short 映射。我可以获得原始字节和短整数,但在对象格式中我只能获得整数。

这是来自here

的源代码
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
    return getInt(columnIndex);

Byte 和 Short 对象类型有什么问题?我如何使用 TINYINT、SMALLINT 等。

实现类似于 getInt

的 getByte 和 getShort 有什么问题

答案在JDBC™ 4.1 Specification JSR 221 第 187 页

Note – The JDBC 1.0 specification defined the Java object mapping for the SMALLINT and TINYINT JDBC types to be Integer. The Java language did not include the Byte and Short data types when the JDBC 1.0 specification was finalized. The mapping of SMALLINT and TINYINT to Integer is maintained to preserve backwards compatibility.

向后兼容 JDBC 规范 1.0 不允许在 JDBC 驱动程序中添加 Byte 和 Short 对象类型。只有一种解决方案:java拐杖。

static public Integer getInteger(final ResultSet rs, final String columnName) throws SQLException {
    final int value = rs.getInt(columnName);
    return rs.wasNull() ? null : value;
}

或者

public <T> T getObjectValue(final ResultSet rs, final String columnName, final Class<T> clazz) throws SQLException {
    final T value = rs.getObject(columnName, clazz);
    return rs.wasNull() ? null : value;
}

public final class ResultSetWrapper {

    private final ResultSet rs;

    public ResultSetWrapper(final ResultSet rs) {
        this.rs = rs;
    }

    public ResultSet getResultSet() {
        return rs;
    }

    public Boolean getBoolean(String label) throws SQLException {
        // ...
    }

    public Byte getByte(String label) throws SQLException {
        // ...
    }

    public Byte getShort(String label) throws SQLException {
        // ...
    }

    // ...

}