Good day. Can anyone explain why JDBC doesn't implement object mapping for some types. For example, Postgres JDBC has no Byte and Short mapping. I can get primitive byte and short, but in object format I can get only Integer.
美好的一天。任何人都可以解释为什么JDBC没有为某些类型实现对象映射。例如,Postgres JDBC没有字节和短映射。我可以得到原始字节和短字,但在对象格式中我只能得到整数。
This is source code from here
这是源代码
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
return getInt(columnIndex);
What's wrong wtih Byte and Short object types? How can I work with TINYINT, SMALLINT and so on.
字节和短对象类型有什么问题?我如何使用TINYINT,SMALLINT等。
What's problem to implement getByte and getShort similar to getInt
实现与getInt类似的getByte和getShort有什么问题
2 个解决方案
#1
3
The answer is in the JDBC™ 4.1 Specification JSR 221 on page 187
答案在第187页的JDBC™4.1规范JSR 221中
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规范将SMALLINT和TINYINT JDBC类型的Java对象映射定义为Integer。当JDBC 1.0规范最终确定时,Java语言不包括字节和短数据类型。保持SMALLINT和TINYINT到Integer的映射以保持向后兼容性。
#2
0
Backward compatibility to JDBC Specification 1.0 don't let to add Byte and Short object types in JDBC drivers. There is only one solution: java crutch.
与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;
}
Or
要么
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;
}
And
和
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 {
// ...
}
// ...
}
#1
3
The answer is in the JDBC™ 4.1 Specification JSR 221 on page 187
答案在第187页的JDBC™4.1规范JSR 221中
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规范将SMALLINT和TINYINT JDBC类型的Java对象映射定义为Integer。当JDBC 1.0规范最终确定时,Java语言不包括字节和短数据类型。保持SMALLINT和TINYINT到Integer的映射以保持向后兼容性。
#2
0
Backward compatibility to JDBC Specification 1.0 don't let to add Byte and Short object types in JDBC drivers. There is only one solution: java crutch.
与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;
}
Or
要么
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;
}
And
和
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 {
// ...
}
// ...
}