JDBC ResultSet对象类型映射没有Byte还是Short? 为什么只有Integer?

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

这是源代码

case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: return getInt(columnIndex); 

字节和短对象类型有什么问题? 我如何使用TINYINT,SMALLINT等。

实现与getInt类似的getByte和getShort有什么问题

答案在第187页的JDBC™4.1规范JSR 221中

– JDBC 1.0规范将SMALLINT和TINYINT JDBC类型的Java对象映射定义为Integer。 当JDBC 1.0规范最终确定时,Java语言不包括字节和短数据类型。 保持SMALLINT和TINYINT到Integer的映射以保持向后兼容性。

与JDBC Specification 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 getObjectValue(final ResultSet rs, final String columnName, final Class 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 { // ... } // ... }